Home  >  Article  >  Web Front-end  >  jquery's ajax asynchronous request receives and returns json data instance_jquery

jquery's ajax asynchronous request receives and returns json data instance_jquery

WBOY
WBOYOriginal
2016-05-16 16:44:171087browse

jquery's asynchronous ajax request receiving and returning json data method is simple to set up. One is that the server handler returns json data, and the other is that the datatype of the ajax sending setting is set to jsonp format data or json format Both Can.

The code example is as follows:

Copy code 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) {
                                                                                                            appendTo("#resText");
                                                                                                                                     }
});
}) ;



The $.ajax method is as follows:

Copy code

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, assuming that the returned json data has the name attribute
//Sometimes you can directly access data.name or data['name']
//But sometimes, you have to pass var jsonData = eval("(" data.responseText ")"); can be accessed through jsonData.name, and in this case, it needs to be complete instead of success
}
});
$ .ajax(options)



This is the most basic JQuery Ajax method, with only one parameter options, which contains request information and callback function information. Parameter contents are all in the form of key:value pairs, and they are all optional.
The syntax is as follows:

$.ajax({options});

url: (string) The address to send the request, which can be a server page or a WebService action.

type: (string) request method, POST or GET

data: (object) data brought when sending a request to the server. It is in the form of key:value pair, such as: {name:"grayworm",sex:"male"}, if it is an array {works:["work1","work2"]}

dataType: (string) expected return Data type. xml, html, json, text, etc.
beforeSend: (Function) is triggered before sending an ajax request. If false is returned, the request will be canceled. If the asynchronous request needs to display gif animation, the visibility of the corresponding should be set here.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn