1

I've made an AJAX call to my database using

$.post('getdata.php', {passLong: passLong, passLat: passLat},
      function(output){

output holds the JSON which was encoded in getdata.php. I'm trying to cycle through the JSON

     $.each(output, function(){
        console.log(this.CarParkName);
      });

In the console I just get 'undefined'. The jQuery each method is supposed to cycle through the JSON and plot markers on a map, the code worked when I had my PHP in the same file, but since using AJAX to get the data from the database, it doesn't work.

I've tried a few different things with the loop, but can't find out what's wrong.

5
  • Use google's cached version then: webcache.googleusercontent.com/… Commented May 5, 2012 at 19:35
  • What do you think is wrong with the way he is using $.each? Commented May 5, 2012 at 19:40
  • Well, I don't see anything wrong. But the structure you are iterating over may not be what you expect. If its an array-like structure e.g. { "1" : { "CarParkName" : ".." }, "2" : { ...} } then I see nothing wrong with that. If it returns a single object containing an actual array like { "data": [{"CarParkName" : ".."}, {...}] } then you need to iterate over "output.data" but I don't think "each" is your problem. Commented May 5, 2012 at 19:50
  • example of it working with correct structure using $.each like you are jsfiddle.net/GNscE Commented May 5, 2012 at 19:52
  • My JSON is like the latter, i.e [{"Address":"St Nicholas Avenue York Yorkshire","CarParkName":"McArthur Glen Designer Outlet","CarParkRef":"26"}] Commented May 5, 2012 at 19:55

2 Answers 2

1

"My JSON is like the latter, i.e [{"Address":"St Nicholas Avenue York Yorkshire","CarParkName":"McArthur Glen Designer Outlet","CarParkRef":"26"}]"

Based on the comment discussion and your response to the other answer, I think the problem is you are dealing with a string, not a javascript object. You just need to parse the string into an object. (I had assumed you were dealing with data parsed into a javascript object already).

If the string looks exactly like that:

[{"Address":"St Nicholas Avenue York Yorkshire",
"CarParkName":"McArthur Glen Designer Outlet","CarParkRef":"26"}]

then do this:

// wrap as an object first, a plain array is not valid json 
var js = $.parseJSON('{"data":' + output + '}');

$.each(js.data, function() {
    console.log(this.CarParkName);
});

and you should be good.

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

1 Comment

Thanks! I've been working on this for a while and tried parsing as well but obviously didn't do it right. Thanks again.
1

I usually do this:

for(var i in output) {
    console.log(output[i]);
}

1 Comment

Thanks for your answer. This returns characters one at a time from the JSON, how would I access values by their name?

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.