$.ajaxSetup() – 为将来的 AJAX 请求设置默认值(参数与$.ajax相同)
注意:封装了发这个函数的文件必须在每一个需要此默认配置的文件里引用该文件。并且引用在该文件的ajax文件之前。
常用的:
url
规定发送请求的 URL。默认是当前页面的所有的ajax的url
type
规定请求的类型(GET 或 POST)
async
布尔值,表示请求是否异步处理。默认是 true
contentType
发送数据到服务器时所使用的内容类型。默认是:"application/x-www-form-urlencoded"
cache
布尔值,表示浏览器是否缓存被请求页面。默认是 true
data
规定要发送到服务器的数据
dataType
预期的服务器响应的数据类型
timeout
设置本地的请求超时时间(以毫秒计)
beforeSend(xhr)
发送请求前运行的函数
success(result,status,xhr)
当请求成功时运行的函数
error(xhr,status,error)
如果请求失败要运行的函数
complete(xhr,status)
请求完成时运行的函数(在请求成功或失败之后均调用,即在 success 和 error 函数之后)
不常用的:
jsonpCallback
在一个 jsonp 中规定回调函数的名称
$.ajaxSetup({
cache: false,
headers: {
csrfToken: localStorage.getItem('csrfToken') || sessionStorage.getItem('csrfToken'),
},
success: function(result, data1, data2) {
if (data2.getResponseHeader("csrfResult")) {
csrfResult = data2.getResponseHeader("csrfResult");
} else {
return;
}
if (csrfResult == 505 && csrfResult == "505") {
top.location.href = "/index.html";
}
// 获得指定的字段
if (data2.getResponseHeader("csrfToken")) {
csrfToken = data2.getResponseHeader("csrfToken");
} else {
return;
}
if (csrfToken != null && csrfToken != '') {
top.location.href = "/index.html";
localStorage.setItem('csrfToken', csrfToken);
}
},
error: function(xmlhttprequest, textstatus) {
$.busyLoadFull("hide");
if (xmlhttprequest.status == 404) {
// window.location = "/sys/404.html";
top.location.href = "/sys/404.html";
} else if (xmlhttprequest.status == 401 || xmlhttprequest.status == 0) {
// window.location = "/index.html";
top.location.href = "/login.html";
} else if (xmlhttprequest.status == 601 || xmlhttprequest.status == 602 || xmlhttprequest.status == 603) {
// window.location = "/sys/admin/admin.html";
top.location.href = "/sys/admin/admin.html";
} else if (xmlhttprequest.status == 403) {
// window.location = "/sys/forbidden.html";
top.location.href = "/sys/forbidden.html";
}
},
});
};