reading return json_encode php with ajax

Asked

Viewed 134 times

0

I get this return with json_encode by php but I am not able to read it in ajax Success, what I am 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) {

  }
});
  • what error it shows you on the console ?

  • Retorna este erro :Uncaught TypeError: Cannot use 'in' operator to search for '185' in {"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"}

  • True, if there are any errors json might not show correctly, check this line 185 and analyze this word in

  • If this is JSON.. it is WRONG! Use jsonlint to validate your JSON.

  • In addition to fixing your php file, it also tries to use javascript dataType: 'json', JSON for json (lower case)

  • Well I made the corrections but nothing to work, as William said must be wrong this JSON. @Williamasimiliar, which would be the correct format to read?

  • Install the Postman and paste this url there to see or that happens the glue in your browser itself and see what returns

  • @Carloslopes In this case, must be in square brackets.

Show 3 more comments

3 answers

0

Try to return only the data on success and inside you make it:

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

...

At least it works that way with me

0

I don’t think you’re using the variable direction 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 );
     }
  }
});
  • When I use dataType:JSON, there is no error, but nothing appears on the console...

  • edited the answer. try so

  • remains the same, no console appears anything. I think this json return must be wrong.

  • is most likely

0

Here is the solution: Adjusted json to -> {"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);
              });

            }
          });

Thank you all for your suggestions !!!!

  • would not necessarily need to return the date in php, but how nice that it worked.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.