JQuery ajax and jqueryajax
Instance
Load JSON data from test. js and display a name field in JSON data:
$.getJSON("test.js", function(json){ alert("JSON Data: " + json.users[3].name);});
Definition and usage
Load JSON data using http get requests.
In jQuery 1.2, you can use a callback function in the form of JSONP to load JSON data of other domains, such as "myurl? Callback =? ". Will jQuery be replaced automatically? For the correct function name to execute the callback function. Note: The Code after this row will be executed before the callback function is executed.
Syntax
jQuery.getJSON(url,[data],[callback])
Parameters |
Description |
Url |
The URL of the page to be loaded. |
Data |
The Key/value parameter to be sent. |
Callback |
The callback function executed when the load is successful. |
Detailed description
This function is short for Ajax functions and is equivalent:
$.ajax({ url: url, data: data, success: callback, dataType: json});
The data sent to the server can be appended to the URL as a query string. IfDataIf the parameter value is an object (ing), it is converted to a string and URL encoded before being appended to the URL.
PassCallbackThe returned data can be a JavaScript object or an array defined in the JSON structure, and the $. parseJSON () method is used for parsing.
More instance example 1
Load four latest pictures about cats from the Flickr jsonp api:
HTML code:
<div id="images"></div>
JQuery code:
$.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){ $("Example 2
Load JSON data from test. js and add parameters to display a name field in JSON data:
$.getJSON("test.js", { name: "John", time: "2pm" }, function(json){ alert("JSON Data: " + json.users[3].name);});