jquery 的 ajax

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_36774307/article/details/78919989
默认请求参数:
//默认请求参数
  var _options = {
    url: null,  // 请求连接
    type: 'GET',  // 请求类型
    data: null,  // post时请求体
    dataType: 'text',  // 返回请求的类型,有text/json两种
    jsonp: 'callback',  // jsonp请求的标志,一般不改动
    jsonpCallback: 'jsonpCallback',  //jsonp请求的函数名
    async: true,   // 是否异步
    cache: true,   // 是否缓存
    timeout:null,  // 设置请求超时
    contentType: 'application/x-www-form-urlencoded',
    success: null,  // 请求成功回调函数
    fail: null   // 请求失败回调
  }
//几种常见方法

一.
$.ajax({
 url: "test.html",
 cache: false,
 success: function(html)
 {
   $("#results").append(html);
 }
 });


二.
$("#feeds").load("feeds.php", {limit: 25}, function(){
  alert("The last 25 entries in the feed have been loaded");
});


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


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


五.
$.ajax({
            type: "POST",
            url: "ShowProduct.aspx/GetDHList",
            data: '{CategoryId:"' + CategoryId + '"}',
            contentType: "application/json;charset=utf-8",
            dataType: "json",
            success: function (data) {
                var item = eval(data.d);
                var StrHtml = "";
                for (var i in item) {}
           }, async: false
        });


六.
$(document).ready(function(){
  $("#b01").click(function(){
  htmlobj=$.ajax({url:"/jquery/test1.txt",async:false});
  $("#myDiv").html(htmlobj.responseText);
  });
});


七.
$.ajax({ url: "test.html", context: document.body, success: function(){
        $(this).addClass("done");
      }});

八.
$.ajax({
            type: "POST",
            url: "some.php",
            data: "name=John&location=Boston",
            success: function(msg){
                alert( "Data Saved: " + msg );
            }
        });

九:延迟失败成功返回方法:
$.get("test.php")
  .done(function(){ alert("$.get succeeded"); })
  .fail(function(){ alert("$.get failed!"); });

猜你喜欢

转载自blog.csdn.net/weixin_36774307/article/details/78919989