標籤: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的一些總結