Differentiating Between an AJAX Call / Browser Request(区分 AJAX 调用/浏览器请求)
问题描述
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 调用/浏览器请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:区分 AJAX 调用/浏览器请求


基础教程推荐
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01