1

My data is coming from PHP like this:

auth0: Object
"client_id: "1234"
tables: Array[2]
  0: Object
     clients_avail: "demo"
  1: Object
     clients_avail: "4532t78"

EDIT: Here's the actual JSON:

{"auth":{"id":"bob"},"tables":[{"clients_avail":"demo"},{"clients_avail":"4532t78"}]}

The above is retrieved like so:

$.ajax({
        url: 'getit.php',
        type: 'POST',
        dataType: 'json',
        data: {
            id_token: my_token
        },
        success: function(data) {
$.each(full_data, function (data) {
$('#clients_avail').append("<option>" + data.tables.clients_avail + "</option>");
 $('#my_id').text(data.auth.id);
}
});

This is coming back uncaught error:

Uncaught TypeError: Cannot read property 'clients_avail' of undefined

What am I missing here?

12
  • 1
    can you do a console.log(data) as the first line of the success handler and see what is logged in the browser console Commented Feb 25, 2015 at 0:49
  • also see the network tab(developer tools) to see what is the response text and past it here Commented Feb 25, 2015 at 0:49
  • 3
    tables is an array of arrays, try data.tables[0][0].clients_avail to access first one. You should probably organize your PHP output better. Commented Feb 25, 2015 at 1:06
  • 1
    there is a double array in tables value.... why is it Commented Feb 25, 2015 at 1:07
  • 1
    You need to loop over full_data.tables, not over full_data... This would be a lot easier if you posted the actual JSON string you are getting back instead of dump of some sort. Commented Feb 25, 2015 at 1:17

2 Answers 2

1

Since you have an array, you need to iterate

$.ajax({
    url: 'getit.php',
    type: 'POST',
    dataType: 'json',
    data: {
        id_token: my_token
    },
    success: function (data) {
        $.each(data.tables, function (data) {
            $('#clients_avail').append("<option>" + data.clients_avail + "</option>");
        })
        $('#my_id').text(data.auth[0].id);
    }
});

Also in your case the auth is an array of objects, which I think is unnecessary, so try to make your response like {"auth":{"id":"bob"},"tables":[{"clients_avail":"demo"},{"clients_avail":"‌​4532t78"}]}, then $('#my_id').text(data.auth.id);

Sign up to request clarification or add additional context in comments.

Comments

1

the problem is the argument inside your $.each(); try doing it like this.

success: function(data) {
   $.each(data.tables[0], function (k, val) {
      $('#clients_avail').append("<option>" + val.clients_avail + "</option>");
      $('#my_id').text(data.auth.id);
   }

I as what skobaljic told you you should organize your json or use json_encode() to output your data.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.