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.