Jquery ajax asynchronous request receiving and returning json data instance _ jquery-js tutorial

Source: Internet
Author: User
Jquery's ajax Asynchronous Method for receiving and returning json data is easy to set. One is that the server processing program returns json data, the other is that the datatype set for ajax sending is set to jsonp or json. This article provides an example. For more information, see jquery's ajax Asynchronous Method for receiving and returning json data, One is that the server processing program returns json data, and the other is that the datatype set for ajax sending is set to jsonp format data or json formatYes.

The sample code is as follows:

The Code is as follows:


$ ('# Send'). click (function (){
$. Ajax ({
Type: "GET ",
Url: "a. php ",
DataType: "jsonp ",
Success: function (data ){
$. Each (data. items, function (I, item ){
$ (""). Attr ("src", item. media. m). appendTo ("# resText ");
If (I = 3 ){
Return false;
}
});
}
});
});

$. Ajax method:

The Code is as follows:


$. Ajax ({
Type: "POST ",
Url: ctxRoot + 'folderaction! SaveInformSetting. action ',
Data: 'jsonstr = '+ inform_settingListStr,
DataType: "json ",
Complete: function (data ){
// Do something here. Assume that the returned json data contains the name attribute.
// Sometimes you can directly access data. name or data ['name ']
// But sometimes, you need to use var jsonData = eval ("(" + data. responseText + ")"); To use jsonData. name access, and in this case, it must be complete instead of success
}
});
$. Ajax (options)

This is the most fundamental JQuery Ajax method. There is only one parameter, options, which contains the request information and callback function information. The parameter content is in the key: value Pair format and is optional.

Syntax:

$. Ajax ({options });
Url: (string) The request sending Address, which can be a server page or WebService action.
Type :( string) Request Method, POST or GET
Data: (object) data that is included when a request is sent to the server. Is the key: value Pair form, such as: {name: "grayworm", sex: "male"}. If it is an array {works: ["work1", "work2"]}
DataType: (string) the expected data type. Xml, html, json, text, etc.
BeforeSend :( Function) is triggered before an ajax request is sent. If false is returned, the request is canceled. If the GIF animation needs to be displayed for an asynchronous request, set the corresponding visibility here.

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.