jQuery 我可以从 ajax 请求中删除 X-Requested-With 标头吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3372962/
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
can i remove the X-Requested-With header from ajax requests?
提问by mkoryak
I wanted to know if anyone has had experience with trying to remove the 'X-Requested-With' header from the ajax request made by jquery (or plain JS). is it possible?
我想知道是否有人曾尝试从 jquery(或普通 JS)发出的 ajax 请求中删除“X-Requested-With”标头。是否可以?
2nd part: do you know if Grease Monkey's ajax requests set this header?
第二部分:你知道Grease Monkey的ajax请求是否设置了这个header?
Thanks
谢谢
header looks like this:
标题看起来像这样:
X-Requested-With XMLHttpRequest
采纳答案by Brock Adams
"2nd part: do you know if Grease Monkey's ajax requests set this header?"
“第二部分:你知道Grease Monkey的ajax请求是否设置了这个header?”
No, Greasemonkey's GM_xmlhttpRequest()
does not set this header (although you can certainly add it).
不,GreasemonkeyGM_xmlhttpRequest()
不会设置此标头(尽管您当然可以添加它)。
The default request issued by GM_xmlhttpRequest()
looks just like a normal browser request.
For example:
由 发出的默认请求GM_xmlhttpRequest()
看起来就像一个普通的浏览器请求。
例如:
GM_xmlhttpRequest
({
method: "GET",
url: "http://google.com/",
onload: function(response) {alert(response.responseText); }
});
Looks like this to my packet sniffer:
我的数据包嗅探器看起来像这样:
GET / HTTP/1.1
Request Method: GET
Request URI: /
Request Version: HTTP/1.1
Host: google.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: UTF-8,*
Keep-Alive: 115
Connection: keep-alive
Cookie: blah, blah, blah, blah, blah...
回答by Synexis
The solution for removing the header in jQuery proposed by @vamp is on the right track, but as others have stated it will still result in an empty X-Requested-With header being sent.
@vamp 提出的在 jQuery 中删除标头的解决方案是正确的,但正如其他人所说,它仍然会导致发送一个空的 X-Requested-With 标头。
The beforeSend callback receives jQuery'sXHR object (jqXHR), rather than the actual XMLHttpRequest object (xhr), which is not even instantiated until after beforeSend is called.
beforeSend 回调接收jQuery 的XHR 对象 (jqXHR),而不是实际的 XMLHttpRequest 对象 (xhr),它甚至在 beforeSend 被调用之后才被实例化。
The setRequestHeader method in jqXHR adds headers to an object, which is then iterated later using the xhr method of the same name, just after adding the X-Requested-With entry to the headers object.
jqXHR 中的 setRequestHeader 方法向对象添加标头,然后在将 X-Requested-With 条目添加到标头对象后,稍后使用同名的 xhr 方法对其进行迭代。
Here's the part in jQuery where this is happening:
这是 jQuery 中发生这种情况的部分:
if ( !options.crossDomain && !headers["X-Requested-With"] ) {
headers["X-Requested-With"] = "XMLHttpRequest";
}
for ( i in headers ) {
xhr.setRequestHeader( i, headers[ i ] );
}
Which leads to the problem: If you don't specify the X-Requested-With header, then jQuery will (unless the crossDomain setting evaluates false, but that may not be the desired solution). It then immediately sets the xhr headers, which can not be unset.
这导致了问题:如果您不指定 X-Requested-With 标头,则 jQuery 会(除非 crossDomain 设置评估为 false,但这可能不是所需的解决方案)。然后立即设置无法取消设置的 xhr 标头。
To prevent sending the X-Requested-With header with jQuery.ajax:
要防止使用 jQuery.ajax 发送 X-Requested-With 标头:
jQuery.ajax provides a setting, xhr, which overrides jQuery's built-in factory method for creating the XMLHttpRequest object. By wrapping this factory method, and then wrapping the browser's native setRequestHeader method, the call from jQuery to set the X-Requested-With header can be ignored.
jQuery.ajax 提供了一个设置 xhr,它覆盖了 jQuery 用于创建 XMLHttpRequest 对象的内置工厂方法。通过包装这个工厂方法,然后包装浏览器的原生 setRequestHeader 方法,来自 jQuery 的设置 X-Requested-With 标头的调用可以被忽略。
jQuery.ajax({
url: yourAjaxUrl,
// 'xhr' option overrides jQuery's default
// factory for the XMLHttpRequest object.
// Use either in global settings or individual call as shown here.
xhr: function() {
// Get new xhr object using default factory
var xhr = jQuery.ajaxSettings.xhr();
// Copy the browser's native setRequestHeader method
var setRequestHeader = xhr.setRequestHeader;
// Replace with a wrapper
xhr.setRequestHeader = function(name, value) {
// Ignore the X-Requested-With header
if (name == 'X-Requested-With') return;
// Otherwise call the native setRequestHeader method
// Note: setRequestHeader requires its 'this' to be the xhr object,
// which is what 'this' is here when executed.
setRequestHeader.call(this, name, value);
}
// pass it on to jQuery
return xhr;
},
success: function(data, textStatus, jqXHR) {
// response from request without X-Requested-With header!
}
// etc...
});
回答by vamp
why not? try:
为什么不?尝试:
(function(){
$.ajaxSettings.beforeSend=function(xhr){
xhr.setRequestHeader('X-Requested-With', {toString: function(){ return ''; }});
};
})(jQuery);
good luck!
祝你好运!
回答by f.ardelian
To do this with jQuery, set your request as cross-domain. Example:
要使用 jQuery 执行此操作,请将您的请求设置为跨域。例子:
server.php
服务器.php
<?='<pre>'.print_r($_SERVER,1);?>
client.js
客户端.js
$.ajax({ url: 'server.php', crossDomain: true }).success(function(r){document.write(r)})
回答by Nick Craver
jQuery doesn't expose a method to do this at the moment, there was a ticket on it a while backrelated to Firefox errors, but rather than making it an option, they fixed the error issue in Firefox.
jQuery 目前没有公开执行此操作的方法,不久前有一张与 Firefox 错误相关的票证,但他们没有将其作为一个选项,而是修复了 Firefox 中的错误问题。
If you're curious, you can see where it's added here, but you can't remove it without editing/overriding jQuery core: http://github.com/jquery/jquery/blob/master/src/ajax.js#L370
如果你很好奇,你可以在这里看到它的添加位置,但你不能在不编辑/覆盖 jQuery 核心的情况下删除它:http: //github.com/jquery/jquery/blob/master/src/ajax.js# L370
回答by Marin Purgar
You may consider this:
你可以考虑这个:
$.ajax({
url: 'http://fiddle.jshell.net/favicon.png',
beforeSend: function( xhr ) {
xhr.setRequestHeader('X-Requested-With', {toString: function(){ return ''; }});
},
success: function( data ) {
if (console && console.log){
console.log( 'Got data without the X-Requested-With header' );
}
}
});