ajax設(shè)置header指南教程
什么是 AJAX?
AJAX = Asynchronous JavaScript And XML.
AJAX 并非編程語言。
AJAX 僅僅組合了:
瀏覽器內(nèi)建的 XMLHttpRequest 對象(從 web 服務(wù)器請求數(shù)據(jù))
JavaScript 和 HTML DOM(顯示或使用數(shù)據(jù))
Ajax 是一個令人誤導(dǎo)的名稱。Ajax 應(yīng)用程序可能使用 XML 來傳輸數(shù)據(jù),但將數(shù)據(jù)作為純文本或 JSON 文本傳輸也同樣常見。
Ajax 允許通過與場景后面的 Web 服務(wù)器交換數(shù)據(jù)來異步更新網(wǎng)頁。這意味著可以更新網(wǎng)頁的部分,而不需要重新加載整個頁面。
下面介紹下ajax設(shè)置header指南教程,內(nèi)容如下所示:
setting參數(shù) headers
$.ajax({
headers: {
Accept: "application/json; charset=utf-8"
},
type: "get",
success: function (data) {
}
});beforeSend設(shè)置header
$.ajax({
type: "GET",
url: "default.do",
beforeSend: function(request) {
request.setRequestHeader("Test", "Chenxizhang");
},
success: function(result) {
alert(result);
}
});$.ajaxSetup()全局設(shè)置Header請求頭
// 設(shè)置請求默認(rèn)值
$.ajaxSetup({
beforeSend: function (xhr) { //可以設(shè)置自定義標(biāo)頭
// 將token塞進(jìn)Header里
xhr.setRequestHeader('Authorization', 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9');
xhr.setRequestHeader('Content-Type', 'application/json'); // application/x-www-form-urlencoded
},
complete: function (xhr) {
// 設(shè)置登陸攔截
if (xhr.responseJSON.code == "error_unauth") {
console.log("沒有登錄!");
layer.msg("沒有登錄!");
// location.href="login.html" rel="external nofollow" rel="external nofollow" ;
} else {
console.log("已經(jīng)登錄!");
}
},
});或
// 設(shè)置請求默認(rèn)值
$.ajaxSetup({
headers: { // 默認(rèn)添加請求頭
"Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9" ,
"Content-Type": "application/json"
} ,
complete: function (xhr) {
// 設(shè)置登陸攔截
if (xhr.responseJSON.code == "error_unauth") {
console.log("沒有登錄!");
layer.msg("沒有登錄!");
// location.href="login.html" rel="external nofollow" rel="external nofollow" ;
} else {
console.log("已經(jīng)登錄!");
}
},
});到此這篇關(guān)于ajax設(shè)置header的文章就介紹到這了,更多相關(guān)ajax設(shè)置header內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于Ajax技術(shù)實現(xiàn)文件上傳帶進(jìn)度條
這篇文章主要介紹了基于Ajax技術(shù)實現(xiàn)文件上傳帶進(jìn)度條的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-06-06
ajax請求后臺得到j(luò)son數(shù)據(jù)后動態(tài)生成樹形下拉框的方法
今天小編就為大家分享一篇ajax請求后臺得到j(luò)son數(shù)據(jù)后動態(tài)生成樹形下拉框的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08
ajax 動態(tài)傳遞jsp等頁面使用id辨識傳遞對象
本文為大家介紹下使用ajax動態(tài)傳遞jsp等頁面,js的jax編寫,使用id辨識傳遞對象2014-01-01

