May 08, 2021 AJAX
In jQuery, there are four main ways AJAX can request:
$.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]);
}
}
});
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);
});
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("添加成功!");
}
})
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: