Home > Article > Web Front-end > Introduction to 4 common request methods of ajax in jQuery
AJAX is a technology for exchanging data with the server. This article will share with you four commonly used ajax request methods. Friends who are interested should take a look.
AJAX is a technology for exchanging data with the server. , you can update a portion of a web page without adding to the entire page.
Four common request methods for ajax in jQuery:
##1.$.ajax() returns the XMLHttpRequest object it created .
$.ajax() has only one parameter: parameter key/value object, including each configuration and callback function information. See detailed parameter options below. If you specify the dataType option, make sure the server returns the correct MIME information (such as xml returns "text/xml"). Example:Save data to the server and display information when successful.$.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 through remote HTTP GET request.
This is a simple GET request function to replace the complex $.ajax. The callback function can be called when the request is successful. If you need to execute a function on error, use $.ajax. Example:$.get("test.cgi", { name: "John", time: "2pm" }, function(data){ alert("Data Loaded: " + data); });
3. Load information through remote HTTP POST request.
This is a simple POST request function to replace the complex $.ajax. The callback function can be called when the request is successful. If you need to execute a function on error, use $.ajax. Example:$.post("/Resources/addfriend.ashx", { "fid": fids, "fname": fnames, "tuid": tuids, "tuname": tunames }, function (data) { if (data == "ok") { alert("添加成功!"); } })
4. Load JSON data through HTTP GET request.
Example:$.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 what I compiled for everyone. I hope it will be helpful to everyone in the future. Related articles:
How to use native ajax to process json strings
Talk about your views and understanding of Ajax submission form
The above is the detailed content of Introduction to 4 common request methods of ajax in jQuery. For more information, please follow other related articles on the PHP Chinese website!