Getjson () Usage instance in Jquery+ajax _jquery

Source: Internet
Author: User

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);
});

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.