Home >Web Front-end >JS Tutorial >What are the request methods of ajax in jQuery?
This time I will bring you the request methods of ajax in jQuery, and the request methods of ajax in jQuery. What are the precautions? The following is a practical case, let's take a look.
AJAX is a technology for exchanging data with the server, which can update part of the web page while supplementing 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 andcallback 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; }); });I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website! Recommended reading:
Ajax obtains API data for national weather forecast
Ajax and JSON data interactive storage
The above is the detailed content of What are the request methods of ajax in jQuery?. For more information, please follow other related articles on the PHP Chinese website!