Home > Web Front-end > JS Tutorial > body text

What are the request methods of ajax in jQuery?

php中世界最好的语言
Release: 2018-04-03 11:34:13
Original
1948 people have browsed it

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 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]);
}
}
});
Copy after login

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);
});
Copy after login

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("添加成功!");
}
})
Copy after login

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;
});
});
Copy after login
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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!