$.ajax的一些總結

來源:互聯網
上載者:User

標籤:htm   代碼   解決   解決辦法   not found   緩衝   ros   data   blog   

1.$.ajaxSetup()函數來全域設定
    $.ajaxSetup({
      url: "/xmlhttp/",
      global: false,
      type: "POST"
    });
    $.ajax({ data: myData });
 所有的ajax請求都共用ajaxSetup裡設定的參數;
2.禁止使用緩衝  cache:false;
3.同步請求
 var html = $.ajax({
  url: "some.php",
  async: false
 }).responseText;
4.beforeSend事件  在發送請求之前觸發,可以修改ajax對象參數,如果return false,本次請求取消;
5.context   可以指定一個DOM元素,在回呼函數裡邊this就指向那個DOM元素;
6.crossDomain   同域請求為false,跨域請求為true,如果你想強制跨域請求(如JSONP形式)同一域,設定crossDomain為true。這使得例如,伺服器端重新導向到另一個域
7.statusCode      一組數值的HTTP代碼和函數對象,當響應時調用了相應的代碼。例如,如果響應狀態是404,將觸發以下警報:
    $.ajax({
      statusCode: {404: function() {
        alert(‘page not found‘);
      }
    });
8.processData   如果要發送 DOM 樹資訊或其它不希望轉換的資訊,請設定為 false。

 

Ajax跨域請求COOKIE無法帶上的解決辦法

 

原生ajax請求方式:
var xhr = new XMLHttpRequest();  xhr.open("POST", "http://xxxx.com/demo/b/index.php", true);  xhr.withCredentials = true; //支援跨域發送cookiesxhr.send();

jquery的ajax的post方法請求:
 $.ajax({                    type: "POST",                    url: "http://xxx.com/api/test",                    dataType: ‘jsonp‘,                    xhrFields: {                        //支援跨域發送cookies                        withCredentials: true                    },                    //強制跨域請求                    crossDomain: true,                    success: function () {                    },                    error: function () {                    }                })
伺服器端設定:
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Origin: http://www.xxx.com");

 

$.ajax的一些總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.