Instance
Load JSON data from Test.js and display a name field data from the JSON data:
Copy Code code as follows:
$.getjson ("Test.js", function (JSON) {
Alert ("JSON Data:" + json.users[3].name);
});
Definitions and usage
Load JSON data through 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 JSONP form. Will jQuery be replaced automatically? To the correct function name to execute the callback function. Note: The code after this line will execute before the callback function executes.
Grammar
Jquery.getjson (Url,[data],[callback])
Parameter description
The URL of the URL to be loaded into the page.
Data to be sent Key/value parameters.
Callback the callback function that is executed when the load succeeds.
Detailed description
This function is shorthand for Ajax functions, equivalent to:
Copy Code code as follows:
$.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-coded before being appended to the URL.
The return data passed to the callback can be a JavaScript object or an array defined in the JSON structure and parsed using the $.parsejson () method.
More examples
Example 1
Download 4 of the latest pictures of cats from the Flickr JSONP API:
HTML Code:
Copy Code code as follows:
<div id= "Images" ></div>
JQuery Code:
Copy Code code as follows:
$.getjson ("Http://api.flickr.com/services/feeds/photos_public.gne?")
Tags=cat&tagmode=any&format=json&jsoncallback=? ", function (data) {
$.each (Data.items, function (I,item) {
$ ("if (i = = 3) return false;
});
});
Example 2
Load JSON data from test.js, attach parameters, display a Name field data in JSON data:
Copy Code code as follows:
$.getjson ("Test.js", {name: "John", Time: "2pm"}, function (JSON) {
Alert ("JSON Data:" + json.users[3].name);
});