Instance
Load JSON data from Test.js and display a name field data from the JSON data:
function (JSON) { alert ("JSON Data:" + json.users[3].name);});
Definition and usage
The JSON data is loaded via an HTTP GET request.
In JQuery 1.2, you can load JSON data for other domains, such as "myurl?callback=?", by using a callback function in the form of JSONP. Will jQuery be replaced automatically? is the correct function name to execute the callback function. Note: The subsequent code of this line will be executed before this callback function executes.
Grammar
Jquery.getjson (Url,[data],[callback])
Parameters |
Description |
Url |
The URL address of the page to be loaded. |
Data |
The Key/value parameter to be sent. |
Callback |
The callback function to execute when loading succeeds. |
Detailed description
This function is shorthand for the Ajax function, which is equivalent to:
$.ajax ({ url:url, data:data, success:callback, Datatype:json});
Data sent to the server can be appended to the URL as a query string. If The value of the data parameter is an object (map), it is converted to a string and URL-encoded before being appended to the URL.
The return data passed to callback can be a JavaScript object, or an array defined in a JSON structure, and parsed using the $.parsejson () method.
More examples of examples 1
Load 4 new pictures of cats from the Flickr JSONP API:
HTML Code:
<id= "Images"></div>
JQuery Code:
$.getjson ("Http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json &jsoncallback=? function (data) { function(i,item) { $ ("); if return false ; });});
Example 2
Load JSON data from test.js, attach parameters, display a Name field data in JSON data:
function (JSON) { alert ("JSON Data:" + json.users[3].name);});
JQuery Ajax-getjson () Usage Example