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 调用/浏览器请求


基础教程推荐
- 如何添加到目前为止的天数? 2022-01-01
- npm start 错误与 create-react-app 2022-01-01
- Bokeh Div文本对齐 2022-01-01
- Fabric JS绘制具有活动形状的多边形 2022-01-01
- Bootstrap 模态出现在背景下 2022-01-01
- 在 contenteditable 中精确拖放 2022-01-01
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
- 检查 HTML5 拖放文件类型 2022-01-01
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01
- fetch 是否支持原生多文件上传? 2022-01-01