我可以从ajax请求中删除X-Requested-With标头吗?
我想知道是否有人尝试从jquery(或普通JS)发出的ajax请求中删除’X-Requested-With’标头。 可能吗?
第二部分:你知道Grease Monkey的ajax请求是否设置了这个标题?
谢谢
标题看起来像这样:
X-Requested-With XMLHttpRequest
“第二部分:你知道Grease Monkey的ajax请求是否设置了这个标题?”
不, Greasemonkey的GM_xmlhttpRequest()
没有设置这个标题(虽然你当然可以添加它)。
GM_xmlhttpRequest()
发出的默认请求看起来就像普通的浏览器请求。
例如:
GM_xmlhttpRequest ({ method: "GET", url: "http://google.com/", onload: function(response) {alert(response.responseText); } });
对我的数据包嗅探器看起来像这样:
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...
删除@vamp提出的jQuery中的头的解决方案是在正确的轨道上,但正如其他人所说,它仍然会导致发送一个空的X-Requested-With头。
beforeSend回调接收jQuery的 XHR对象(jqXHR),而不是实际的XMLHttpRequest对象(xhr),直到调用beforeSend之后才对其进行实例化。
jqXHR中的setRequestHeader方法将标题添加到对象中,稍后使用相同名称的xhr方法对其进行迭代,就在将X-Requested-With条目添加到headers对象之后。
这是jQuery中发生这种情况的部分:
if ( !options.crossDomain && !headers["X-Requested-With"] ) { headers["X-Requested-With"] = "XMLHttpRequest"; } for ( i in headers ) { xhr.setRequestHeader( i, headers[ i ] ); }
这导致了问题:如果您没有指定X-Requested-With标头,则jQuery将(除非crossDomain设置评估为false,但这可能不是所需的解决方案)。 然后立即设置xhr标头,不能取消设置。
要防止使用jQuery.ajax发送X-Requested-With标头:
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... });
为什么不? 尝试:
(function(){ $.ajaxSettings.beforeSend=function(xhr){ xhr.setRequestHeader('X-Requested-With', {toString: function(){ return ''; }}); }; })(jQuery);
祝好运!
要使用jQuery执行此操作,请将您的请求设置为跨域。 例:
server.php
='
'.print_r($_SERVER,1);?>client.js
$.ajax({ url: 'server.php', crossDomain: true }).success(function(r){document.write(r)})
jQuery目前没有公开一种方法来执行此操作, 有一段时间它与Firefox错误有关,但它没有使它成为一个选项,他们修复了Firefox中的错误问题。
如果你很好奇,你可以在这里看到它的添加位置,但是如果不编辑/覆盖jQuery核心就不能删除它: http : //github.com/jquery/jquery/blob/master/src/ajax.js# L370
你可以考虑这个:
$.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' ); } } });