JQuery ajax and jqueryajax

Source: Internet
Author: User

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

 

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.