概述
如果这是为了调试目的,那么您只需使用Firebug或Chrome Developer Tools(以及IE中的所有功能)来检查从浏览器到服务器的网络流量。
一个替代方案是使用像这样的脚本:
$.ajax({
url: 'someurl',
headers:{'foo':'bar'},
complete: function() {
alert(this.headers.foo);
}
});
但是,我认为只有标头中已经定义的头可用(不知道如果标题被更改(例如在BeforeSend中)会发生什么。
编辑:如果你想在XMLHttpRequest的所有调用setRequestHeader上捕获头文件,那么你只需要写入该方法即可。这是一个黑客,当然,你需要确保在任何请求发生之前运行包装代码的函数。
// 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 exist
if(!this.headers) {
this.headers = {};
}
// Create a list for the header that if it does not exist
if(!this.headers[header]) {
this.headers[header] = [];
}
// Add the value to the header
this.headers[header].push(value);
}
现在,一旦在XMLHttpRequest实例上设置了标题,我们可以通过检查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'
最后
以上就是漂亮水杯为你收集整理的h5获取http请求头_javascript – JS/jQuery获取HTTPRequest请求头?的全部内容,希望文章能够帮你解决h5获取http请求头_javascript – JS/jQuery获取HTTPRequest请求头?所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复