Berntsson
Berntsson

Reputation: 33

jquery ajax: newbie question

  1. A file called test containing the following line:

    [{"foo":"abc","bar":"01"},{"foo":"def","bar":"02"}]

  2. jquery ajax:

    $.ajax({ url: "test", success: function(data) {....

Stupid question maybe, but shouldnt i be able to get the values in the function with something like data.foo ? Never used ajax before as you might have figured out :)

Upvotes: -1

Views: 73

Answers (2)

Daniel
Daniel

Reputation: 704

In your specific case, you can't exactly call data.foo because your data object is actually an array of objects, so you would access its properties with data[0].foo, data[1].foo and so on.

Upvotes: 1

amosrivera
amosrivera

Reputation: 26514

Yes, you would be able, the [{"foo":"abc","bar":"01"},{"foo":"def","bar":"02"}] data structure is called json and jQuery parses it without problem, it even has a specific method for json jQuery.getJSON. See examples here:

http://pinoytech.org/blog/post/How-to-Use-JSON-with-jQuery-AJAX

http://viralpatel.net/blogs/2009/04/jquery-ajax-tutorial-example-ajax-jquery-development.html

Upvotes: 0

Related Questions