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

What are the request methods of ajax? Detailed explanation of 4 commonly used request methods in Ajax

寻∝梦
Release: 2018-09-10 15:13:10
Original
6259 people have browsed it

This article mainly introduces you to the 4 commonly used request methods about ajax. Now let us take a look at the four types.

1.$.ajax()返回其创建的 XMLHttpRequest 对象。
$.ajax() 只有一个参数:参数 key/value 对象,包含各配置及回调函数信息。详细参数选项见下。
如果你指定了 dataType 选项,请确保服务器返回正确的 MIME 信息,(如 xml 返回 "text/xml")。
实例:
保存数据到服务器,成功时显示信息。
Copy after login
$.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.通过远程 HTTP GET 请求载入信息。
这是一个简单的 GET 请求功能以取代复杂 $.ajax 。请求成功时可调用回调函数。如果需要在出错时执行函数,请使用 $.ajax。
实例:
Copy after login
$.get("test.cgi", { name: "John", time: "2pm" },
  function(data){
    alert("Data Loaded: " + data);
});
Copy after login
3. 通过远程 HTTP POST 请求载入信息。(想看更多就到PHP中文网栏目中学习)
这是一个简单的 POST 请求功能以取代复杂 $.ajax 。请求成功时可调用回调函数。如果需要在出错时执行函数,请使用 $.ajax。
实例:
Copy after login
$.post("/Resources/addfriend.ashx", { "fid": fids, "fname": fnames, "tuid": tuids, "tuname": tunames }, function (data) {
    if (data == "ok") {
        alert("添加成功!");
    }
})
Copy after login
4.通过 HTTP GET 请求载入 JSON 数据。
实例:
Copy after login
$.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

This article ends here (if you want to see more, go to the PHP Chinese website AJAX User Manual column to learn). If you have any questions, you can Leave a question below.

The above is the detailed content of What are the request methods of ajax? Detailed explanation of 4 commonly used request methods in Ajax. 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!