Home  >  Article  >  Web Front-end  >  What are the request methods of ajax? Detailed explanation of 4 commonly used request methods in Ajax

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

寻∝梦
寻∝梦Original
2018-09-10 15:13:106248browse

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")。
实例:
保存数据到服务器,成功时显示信息。
$.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.通过远程 HTTP GET 请求载入信息。
这是一个简单的 GET 请求功能以取代复杂 $.ajax 。请求成功时可调用回调函数。如果需要在出错时执行函数,请使用 $.ajax。
实例:
$.get("test.cgi", { name: "John", time: "2pm" },
  function(data){
    alert("Data Loaded: " + data);
});
3. 通过远程 HTTP POST 请求载入信息。(想看更多就到PHP中文网栏目中学习)
这是一个简单的 POST 请求功能以取代复杂 $.ajax 。请求成功时可调用回调函数。如果需要在出错时执行函数,请使用 $.ajax。
实例:
$.post("/Resources/addfriend.ashx", { "fid": fids, "fname": fnames, "tuid": tuids, "tuname": tunames }, function (data) {
    if (data == "ok") {
        alert("添加成功!");
    }
})
4.通过 HTTP GET 请求载入 JSON 数据。
实例:
$.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;
  });
});

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!

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