Home>Article>Web Front-end> There are several ajax request methods

There are several ajax request methods

王林
王林 Original
2020-05-10 11:06:51 5015browse

There are several ajax request methods

1. $.ajax() returns the XMLHttpRequest object it created

$.ajax() has only one parameter: parameter key/value Object, including configuration and callback function information.

If you specify the dataType option, you need to ensure that 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

Compared with the complex $.ajax, the GET request function is simpler and the request is successful. The callback function can be called when. Of course, if you need to execute the function when an error occurs, please also use $.ajax.

Example:

$.get("test.cgi", { name: "John", time: "2pm" },function(data){ alert("Data Loaded: " + data); });

3. Load information through remote HTTP POST request

The POST request function is also relatively simple and can be called when the request is successful. Callback. If you need to execute a function when an error occurs, use $.ajax request.

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){ $("").attr("src", item.media.m).appendTo("#images"); if ( i == 3 ) return false; }); });

Recommended tutorial:js Getting Started Tutorial

The above is the detailed content of There are several ajax request methods. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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