1

I am trying to get a Ajax-call made from jQuery to return a JavaScript array generated by PHP, but can't get it accessible. Here is what I am doing:

  1. I run the following code onLoad: $(function() { jQuery.ajax({ url: 'retrieve.php', success: function(data) { }, async: false }, "script");

  2. retrieve.php returns the following (retrieve.php does the formatting, and echoes it out): var data = { "info" : [{ "title": "Title", "description": "Description" }], "playlist" : [ { "title": "In This Place", "subTitle": "Excalibur", "href": "http://localhost/manger/ny/#desemberkonsert_in-this-place", "url": "../flv/desemberkonsert/21_in_this_place.flv", "thumbnail": "../flv/desemberkonsert/21_in_this_place_thumbnail.png", "time": "5:39" }, { "title": "Black Hole", "subTitle": "Excalibur", "href": "http://localhost/manger/ny/#desemberkonsert_black-hole", "url": "../flv/desemberkonsert/22_black_hole.flv", "thumbnail": "../flv/desemberkonsert/22_black_hole_thumbnail.png", "time": "4:26" }] }

  3. I then try to access this code using for example: $("#placeholder").text(data.playlist[0]["title"]);

  4. This returns as "undefined"

The array does work when I copy and paste it directly onto the file, but when returned as a callback it fails. How do I access the returned array?

2
  • Why not return this as JSON? Additionally your PHP is going to have to set the appropriate content-type header Commented Feb 5, 2011 at 19:47
  • I tried switching it to json in the ajax-call (also removed 'var data = ' from retrieve.php), which content-type would I set it to? Is there a specific one for json? Commented Feb 5, 2011 at 20:39

2 Answers 2

1

You should return just the object, not a string of JavaScript which assigns the object to a name. That's NOT a JSON response.

Your code which uses the object should be in the success handler of the ajax call, not separate code somewhere else.

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

3 Comments

I removed the 'var data = ' part of it, and moved my $("#placeholder").text(data.playlist[0]["title"]); code into the callback (also told the ajax call to return "json"). It does still return "undefined" however, what is going wrong?
Time to learn to use a javascript debugger, like firebug or the developer tools built into chrome/ie.
I am using Firebug, and the "data" response from retrieve.php is valid enough, but I still cannot access it within the callback (as for example $("#placeholder").text(data.playlist[0]["title"]);)
0

You probably need to access your data with the [] notation, and not the . object notation:

data["playlist"][0]["title"]

1 Comment

This appears to make no difference in regards to results, both will return "undefined".

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.