Javascript JS/jQuery 获取 HTTPRequest 请求标头?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10515862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JS/jQuery get HTTPRequest request headers?
提问by Lwyrn
Using getAllResponseHeaders in the xhr object, is possible to get all the response headers after an ajax call. But I can't found a way to get the Request headers string, is that possible ?
在 xhr 对象中使用 getAllResponseHeaders 可以在 ajax 调用后获取所有响应头。但是我找不到获取请求标头字符串的方法,这可能吗?
回答by Emil H
If this is for debugging purposes then you can just use Firebug or Chrome Developer Tools (and whatever the feature is called in IE) to examine the network traffic from your browser to the server.
如果这是出于调试目的,那么您可以只使用 Firebug 或 Chrome 开发人员工具(以及 IE 中调用的任何功能)来检查从浏览器到服务器的网络流量。
An alternative would be to use something like this script:
另一种方法是使用类似这个脚本的东西:
$.ajax({
url: 'someurl',
headers:{'foo':'bar'},
complete: function() {
alert(this.headers.foo);
}
});
However I think only the headers already defined in headers
is available (not sure what happens if the headers are altered (for instance in beforeSend).
但是我认为只有已经定义的头文件headers
可用(不确定如果头文件被改变会发生什么(例如在 beforeSend 中)。
You could read a bit more about jQuery ajax at: http://api.jquery.com/jQuery.ajax/
您可以在以下位置阅读更多关于 jQuery ajax 的信息:http: //api.jquery.com/jQuery.ajax/
EDIT:If you want to just catch the headers on all calls to setRequestHeader on the XMLHttpRequest then you can just wrapp that method. It's a bit of a hack and of course you would need to ensure that the functions wrapping code below is run before any of the requests take place.
编辑:如果您只想捕获 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);
}
Now, once the headers have been set on an XMLHttpRequest
instance we can get them out by examining xhr.headers
e.g.
现在,一旦在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'
回答by Reza S
Something you could to is use Sinon's FakeXMLHttpRequest to replace your browser's XHR. It's described in this documenton how to use it for testing but I'm pretty sure you can use the module for your debugging purposes.
您可以使用 Sinon 的 FakeXMLHttpRequest 来替换浏览器的 XHR。本文档中描述了如何使用它进行测试,但我很确定您可以将该模块用于调试目的。
What you need to do is:
你需要做的是:
var requests;
this.xhr = sinon.useFakeXMLHttpRequest();
this.xhr.onCreate = function(xhr) {
requests.push(xhr);
}
And then later on, you can check your requests
array for headers by:
然后,您可以通过以下方式检查您的requests
数组的标题:
console.log(requests[0].requestHeaders);
To access your request headers.
访问您的请求标头。