網(wǎng)絡(luò)服務(wù)器租賃費(fèi)一般多少錢(qián)seo怎么做
場(chǎng)景:在jquery封裝的ajax請(qǐng)求中,默認(rèn)是異步請(qǐng)求。
需要定一個(gè)秘鑰進(jìn)行解密,所以只能存放在請(qǐng)求頭中。然后需要值的時(shí)候去請(qǐng)求頭中讀取。
注意:dataType設(shè)置,根據(jù)請(qǐng)求參數(shù)的格式設(shè)置,如果是加密字符串,就設(shè)置為text,如果是json,就設(shè)置為json,否則,接口請(qǐng)求狀態(tài)碼是200,但是會(huì)進(jìn)入到error函數(shù)中。
$.ajax({url: 'url',data:{'req':somedata},type:"post",cache:'false',dataType:'json',//注:headers:{'key':'bar'}, //設(shè)置請(qǐng)求頭success: function() {alert(this.headers.key);//獲取請(qǐng)求頭},error:function(){}
});
目前到上面我的問(wèn)題就解決了。
參考文章:https://www.it1352.com/2772303.html
但是我認(rèn)為只有已經(jīng)在 headers 中定義的標(biāo)題是可用的(不確定如果標(biāo)題被改變會(huì)發(fā)生什么(例如在 beforeSend 中).
您可以在以下位置關(guān)于 jQuery ajax 的信息:http://api.jquery.com/jQuery.ajax/
如果您只想捕獲對(duì) XMLHttpRequest 上的 setRequestHeader 的所有調(diào)用的標(biāo)頭,那么您可以包裝該方法.這有點(diǎn)麻煩,當(dāng)然您需要確保在任何請(qǐng)求發(fā)生之前運(yùn)行包裝下面的代碼的函數(shù).
// Reasign the existing setRequestHeader function to
// something else on the XMLHtttpRequest class
XMLHttpRequest.prototype.wrappedSetRequestHeader = XMLHttpRequest.prototype.setRequestHeader; // Override the existing setRequestHeader function so that it stores the headers
XMLHttpRequest.prototype.setRequestHeader = function(header, value) {// Call the wrappedSetRequestHeader function first // so we get exceptions if we are in an erronous state etc.this.wrappedSetRequestHeader(header, value);// Create a headers map if it does not existif(!this.headers) {this.headers = {};}// Create a list for the header that if it does not existif(!this.headers[header]) {this.headers[header] = [];}// Add the value to the headerthis.headers[header].push(value);
}
現(xiàn)在,一旦在 XMLHttpRequest 實(shí)例上設(shè)置了標(biāo)頭,我們就可以通過(guò)檢查 xhr.headers 例如
var xhr = new XMLHttpRequest();
xhr.open('get', 'demo.cgi');
xhr.setRequestHeader('foo','bar');
alert(xhr.headers['foo'][0]); // gives an alert with 'bar'