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

Introduction to 4 common request methods of ajax in jQuery

亚连
Release: 2018-05-23 11:36:27
Original
1928 people have browsed it

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

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

Ajax Get data by city name

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!

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!