2010-07-30 112 views
22

我想知道是否有人嘗試從jquery(或普通的JS)發出的ajax請求中刪除'X-Requested-With'頭部。可能嗎?我可以從ajax請求中刪除X-Requested-With標頭嗎?

第二部分:你知道Grease Monkey的ajax請求是否設置了這個頭文件嗎?

感謝

頭看起來是這樣的:

X-Requested-With XMLHttpRequest 
+0

我認爲擁有這個頭文件集的好處之一是,您可以在服務器端腳本中檢查它,並確定它是否確實是AJAX請求,因此如何處理它? – MrWhite 2010-07-30 15:34:03

+2

w3d:服務器端知道你發出ajax請求的缺點是他們可以阻止你在你的GM腳本中執行它 – mkoryak 2011-02-05 21:52:16

回答

3

「第二部分:你知道,如果油猴的Ajax請求設置了這個頭文件?「

不,Greasemonkey's 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... 
6

爲什麼不呢? 嘗試:

(function(){ 
    $.ajaxSettings.beforeSend=function(xhr){ 
     xhr.setRequestHeader('X-Requested-With', {toString: function(){ return ''; }}); 
    }; 
})(jQuery); 

好運!

+0

不是我真的希望它是瀏覽器特定的,但它對我來說工作正常Firefox 4,Chrome 10,IE 9和Opera 11.對於我所需要的,我不介意改變*所有* AJAX請求,但我想知道是否有辦法讓這種情況只發生在特定的請求上。 – patridge 2011-05-13 14:02:12

+0

你不能添加beforeSend回調到$ .ajax調用本身嗎? – hayavuk 2011-06-06 20:02:57

+0

根據@馬林的[大致相同]的答案,它看起來像你可以。在一個[示例jsFiddle](http://jsfiddle.net/patridge/amA3W/)中,我刪除了'X-Requested-With'頭並且未能刪除'Referer'頭(Chrome不會讓它發生) 。爲了獲得更多樂趣,我還將[這個全要求版本的小提琴]放在一起(http://jsfiddle.net/patridge/h4Lvp/)。 – patridge 2012-03-06 16:01:13

-3

您可以考慮這樣的:

$.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'); 
    } 
    } 
}); 
+0

請注意,@ vamp已經給出了確切的答案 – mkoryak 2011-11-17 15:25:39

+2

雖然'beforeSend'部分是相同的,但這是刪除標頭的更細化的方式(每個請求對所有請求都是@vamp做的)。也就是說,在幫助那些尋求這種解決方案的人的答案中,應該澄清它的不同之處。 – patridge 2012-03-06 15:20:59

+1

不起作用。只將標題的值設置爲空字符串。標題仍然發送。 – 2013-02-21 09:42:54

5

要使用jQuery做到這一點,設置您的請求跨域。例如:

server.php

<?='<pre>'.print_r($_SERVER,1);?> 

client.js

$.ajax({ url: 'server.php', crossDomain: true }).success(function(r){document.write(r)}) 
+3

添加'crossDomain:true'爲我工作,謝謝! – 2014-04-19 14:23:34

+1

這對我試圖訪問Pinterest API使用jQuery(例如https://api.pinterest.com/v3/pidgets/users/USERNAME/pins/) – Tebbers 2016-03-11 13:30:33

18

通過@vamp提出用於去除在jQuery的標頭中的溶液是在正確的軌道上,但作爲其他人表示它仍然會導致發送一個空的X-Requested-With報頭。

的beforeSend回調接收jQuery的 XHR對象(jqXHR),而不是實際的XMLHttpRequest對象(XHR),它甚至沒有實例化,直到beforeSend被調用後。

jqXHR中的setRequestHeader方法將標題添加到一個對象,稍後使用相同名稱的xhr方法在將X-Requested-With條目添加到標頭對象後迭代。

下面是jQuery的部分地方發生這種情況:

if (!options.crossDomain && !headers["X-Requested-With"]) { 
    headers["X-Requested-With"] = "XMLHttpRequest"; 
} 

for (i in headers) { 
    xhr.setRequestHeader(i, headers[ i ]); 
} 

這導致了問題:如果不指定X-要求,隨着頭,然後jQuery將(除非跨域設置評估板錯誤,但這可能不是所需的解決方案)。然後立即設置xhr標題,它不能被取消設置。


爲了防止發送的X請求-隨着頭與jQuery.ajax:

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... 

}); 
+0

我有一個腳本,使跨域請求,應該已經是一個簡單的CORS請求,但由於jQuery自動添加了「X-Requested-With」標頭,這被認爲是自定義的,它使瀏覽器進行了不必要的CORS預檢檢查。 感謝分享這個,我瘋了,試圖找出爲什麼我不能跳過預檢。 – Dooms101 2015-11-30 20:18:16

相關問題