Home >Web Front-end >JS Tutorial >Example of getJSON() usage in jQuery ajax_jquery
Example
Load JSON data from test.js and display a name field data in the JSON data:
Definition and usage
Load JSON data via HTTP GET request.
In jQuery 1.2, you can load JSON data from other domains by using a JSONP-style callback function, such as "myurl?callback=?". jQuery will automatically replace ? with the correct function name to execute the callback function. Note: The code after this line will be executed before this callback function is executed.
Grammar
jQuery.getJSON(url,[data],[callback])
Parameter Description
url The URL address of the page to be loaded.
data Key / value parameters to be sent.
callback The callback function executed when loading is successful.
Detailed description
This function is the abbreviation of Ajax function, which is equivalent to:
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 is parsed using the $.parseJSON() method.
More examples
Example 1
Load the 4 latest cat pictures from the Flickr JSONP API:
HTML code:
jQuery code:
Example 2
Load JSON data from test.js, append parameters, and display a name field data in the JSON data: