問(wèn)題描述
HTTP 請(qǐng)求的標(biāo)頭中是否有任何內(nèi)容可以讓我區(qū)分 AJAX 調(diào)用和來(lái)自給定客戶(hù)端的直接瀏覽器請(qǐng)求?無(wú)論如何,用戶(hù)代理字符串通常相同嗎?
Is there anything in the header of an HTTP request that would allow me to differentiate between an AJAX call and a direct browser request from a given client? Are the user agent strings usually the same regardless?
推薦答案
如果您使用 Prototype、jQuery、Mootools 或 YUI,您應(yīng)該找到一個(gè) X-Requested-With:XMLHttpRequest 標(biāo)頭給你的伎倆.應(yīng)該可以在其他庫(kù)中插入您喜歡的任何標(biāo)題.
If you use Prototype, jQuery, Mootools or YUI you should find a X-Requested-With:XMLHttpRequest header which will do the trick for you. It should be possible to insert whatever header you like with other libraries.
在最低級(jí)別,給定 XMLHttpRequest 或 XMLHTTP 對(duì)象,您可以使用setRequestHeader 方法如下:
At the lowest level, given a XMLHttpRequest or XMLHTTP object, you can set this header with the setRequestHeader method as follows:
xmlHttpRequest.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
這篇關(guān)于區(qū)分 AJAX 調(diào)用/瀏覽器請(qǐng)求的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!