问题描述
HTTP 请求的标头中是否有任何内容可以让我区分 AJAX 调用和来自给定客户端的直接浏览器请求?无论如何,用户代理字符串通常相同吗?
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,您应该找到一个 X-Requested-With:XMLHttpRequest 标头给你的伎俩.应该可以在其他库中插入您喜欢的任何标题.
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.
在最低级别,给定 XMLHttpRequest 或 XMLHTTP 对象,您可以使用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');
这篇关于区分 AJAX 调用/浏览器请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!