JQuery Ajax-getjson () Usage Example

Source: Internet
Author: User

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

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.