开发者

jquery ajax: newbie question

开发者 https://www.devze.com 2023-02-21 20:58 出处:网络
A file called test contai开发者_如何转开发ning the following line: [{\"foo\":\"abc\",\"bar\":\"01\"},{\"foo\":\"def\",\"bar\":\"02\"}]
  1. A file called test contai开发者_如何转开发ning 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 :)


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消