Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

4 common Ajax request methods


May 08, 2021 AJAX


Table of contents


4 common AJAX request methods


In jQuery, there are four main ways AJAX can request:

1, $.ajax() returns the XMLHttpRequest object it created

$.ajax() only one parameter: key/value which contains information for each configuration and callback function.

If you specify the dataType you need to ensure that the server returns the correct MIME information (such as xml returns "text/xml").

Instance:

Save the data to the server and display the information if it succeeds.

$.ajax({
type: "post",
dataType: "html",
url: '/Resources/GetList.ashx',
data: dataurl,
success: function (data) {
if (data != "") {
$("#pager").pager({ pagenumber: pagenumber, pagecount: data.split("$")[1], buttonClickCallback: PageClick });
$("#anhtml").html(data.split("$")[0]);
}
}
});

2. Load information via remote HTTP GET request

The GET request feature is simpler than the complex $.ajax and callback functions can be called if the request is successful. GET Of course, if you need to execute a function in the event of an $.ajax

Instance:

$.get("test.cgi", { name: "John", time: "2pm" },
function(data){
alert("Data Loaded: " + data);
});

3. Load the information via a remote HTTP POST request

POST request function is also relatively simple, and callback functions can be called if the request is successful. If you need to execute a function in the event of an $.ajax request.

Instance:

$.post("/Resources/addfriend.ashx", { "fid": fids, "fname": fnames, "tuid": tuids, "tuname": tunames }, function (data) {
if (data == "ok") {
alert("添加成功!");
}
})

4. Load JSON data via HTTP GET request

Instance:

$.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){
$("<img/>").attr("src", item.media.m).appendTo("#images");
if ( i == 3 ) return false;
});
});

The above is the w3cschool for you to bring 4 common Ajax request method introduction, I hope to help you.


Related tutorials:

AJAX Getting Started tutorial

jQuery tutorial