Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Here's my ajax call:

$.ajax({ 
    url: 'url-to-json',
    type: 'POST',
    dataType: 'json',
    cache: 'false',
    data: { lat: lat, lng: lng }
}).done(function(data) {
    $.each(data, function(a) {
        alert(data[a]);
    });
});

Here's the json it's iterating over:

[
{"Id":"4c75bd5666be6dcb9f70c10f","Name":"BXtra","EnglishName":null,"Lat":35.7515869140625,"Lng":139.33872985839844},

{"Id":"4c5160a1d2a7c9b655d51211","Name":"セブンイレブン 武蔵野台店","EnglishName":null,"Lat":35.750205993652344,"Lng":139.33448791503906},

...
]

But instead of actually giving me access to the properties of each item in the json array, it literally loops through each character in the array, one by one.

What am I doing wrong?

share|improve this question
Try passing a success callback to the .ajax function instead of using .done(). – Blazemonger Jul 5 '12 at 15:49
Thanks for the tip. I tried this too. Still loops through each character of the json result, one-by-one. – Chad Jul 5 '12 at 15:59
1  
I also suspect the problem is with your JSON, then. Are you sure the JSON you're getting is parseable? – Blazemonger Jul 5 '12 at 16:07
Thanks, manually calling $.parseJSON(json) works. I'll have to look closer at the MIME type of the return. – Chad Jul 5 '12 at 16:33

4 Answers

up vote 5 down vote accepted

You can modify your $.each function in two ways:

$.each(data, function(index,el) {
    // el = object in array
    // access attributes: el.Id, el.Name, etc
});

Or,

$.each(data, function() {
    // this = object in array
    // access attributes: this.Id, this.Name, etc
});

If data is a string within your done function and not an object, then you'll need to run

data = $.parseJSON(data)

before your $.each loop

share|improve this answer
I tried this... it appears every el is referencing each individual character of the json... almost as if it were a string, and the loop iterates of each individual character rather than each entry in the array. – Chad Jul 5 '12 at 15:55
Oh, in that case, data is probably coming back as a string, and not as an object. Run alert(typeof data) from within your done function. If it comes back as string, then data isn't being converted into an object. I don't know if .done() uses the dataType attribute of $.ajax or whether it only uses the raw response – jackwanders Jul 5 '12 at 16:01
That was it... even though I set the type to JSON, it wasn't parsing it... thanks! – Chad Jul 5 '12 at 16:31
@Chad did you check the MIME type returned by the server? docs.jquery.com/Specifying_the_Data_Type_for_AJAX_Requests – Christophe Jul 5 '12 at 17:02

Use this to refer to current element inside .each:

$.ajax({ 
    url: 'url-to-json',
    type: 'POST',
    dataType: 'json',
    cache: 'false',
    data: { lat: lat, lng: lng }
}).done(function(data) {
    $.each(data, function() {
        alert(this.Id);
    });
});
share|improve this answer
   
To add explanation, the OP's method was retrieving the object back when using data[a] which still needed to reference a property of that object. OP's method would have been fine if they just modified it to something like data[a].Id, data[a]['Id'], or alike. However, while inside the .each this==object[key], that is to say this==data[a] – Brad Christie Jul 5 '12 at 15:46
using this still iterates over each character of the JSON individually. If I alert(this) it show each character, one by one. If I alert(this.Id) it shows undefined. – Chad Jul 5 '12 at 15:49
1  
I have verified it and it works. Are you sure that you've got correct data in done? – walkhard Jul 5 '12 at 15:54
Appeared even though I set the type as JSON, it wasn't parsing it. – Chad Jul 5 '12 at 16:32

Maybe your server is not returning the right MIME type for JSON ('application/json') and JQuery is interpreting it as just a string?

share|improve this answer

Using success always works for me:

$.ajax({ 
  url: 'url-to-json',
  type: 'POST',
  dataType: 'json',
  cache: 'false',
  data: { lat: lat, lng: lng },
  success: function(data) {
    $.each(data, function() {
      alert(this.Id);
    });
  }
});

Forcing a parse of the JSON would be: jQuery.parseJSON( json ) as a temporary fix if the dataType is playing up...

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.