reading return json_encode php with ajax

0

I get this return with json_encode by php but I can not read it in ajax success, what am I doing wrong?

{"id":"1","id_pesquisa":"3","id_pergunta":"7","id_resposta":"31","mostrar":"1","ocultar":"2"},
{"id":"2","id_pesquisa":"3","id_pergunta":"8","id_resposta":"31","mostrar":"2","ocultar":"1"}

var direcao = $.ajax({
  type : 'get',
  url: 'dadosPS.php?ps=direcionamento',
  dataType: 'JSON',
  beforeSend: function (xhr) {

  },success: function (data, textStatus, jqXHR) {
    for(var i = 0;i<data.length;i++){
      console.log(data[i].id);
    }
  },error: function (jqXHR, textStatus, errorThrown) {

  }
});
    
asked by anonymous 24.08.2017 / 19:23

3 answers

0

Try to return only the data in success and inside you do it:

...
   /* resto do codigo */
    success: function (data) {
       $.each(data, function(i, j)){
         console.log( j.id );
        }
     }

...

At least that works for me

    
24.08.2017 / 19:28
0

I think you're not using the direction variable so I removed it, you can always remove functions other than yours, like this:

$.ajax({
  type : 'GET',
  url: 'dadosPS.php?ps=direcionamento',
  dataType: 'JSON',
  success: function (data) {
     var data = JSON.parse(data);
     $.each(data, function(i, v)){
         console.log( v.id );
     }
  }
});
    
24.08.2017 / 19:36
0

The solution follows: Adjusted json for - > {"data":[{"id":"1","id_pesquisa":"3","id_pergunta":"7","id_resposta":"31","mostrar":"1","ocultar":"2"},{"id":"2","id_pesquisa":"3","id_pergunta":"8","id_resposta":"31","mostrar":"2","ocultar":"1"}]}

//Direcionamnetos
       $.ajax({
            type : 'GET',
            url: 'dadosPS.php?ps=direcionamento',
            dataType: 'json',
            success: function (data) {

               $.each(data.data, function(key, val){
                  console.log(val.id);
              });

            }
          });

To all Thanks for the suggestions !!!!

    
24.08.2017 / 19:57