[翻译]  How do I detect requests initiated by the new fetch standard? How should I detect an AJAX request in general?

[CHINESE]  如何检测新获取标准发起的请求?我应该如何检测一般的AJAX请求?


On a server it can be useful to know that an incoming request is AJAX.

在服务器上,知道传入的请求是AJAX会很有用。

Most js libraries use XMLHttpRequest and so provide HTTP_X_REQUESTED_WITH: XMLHttpRequest, but neither Chrome's implementation nor Github's polyfill of the new fetch uses a similar header. So how can one detect that the request is AJAX?

大多数js库使用XMLHttpRequest,因此提供HTTP_X_REQUESTED_WITH:XMLHttpRequest,但Chrome的实现和Github的新fetch的polyfill都没有使用类似的头。那么怎样才能检测到请求是AJAX?

Why is a request identifying its initiator not enforced through standards for fetch and XMLHttpRequest? Should something else be used for decision-making (e.g. clients providing the content-type they expect in response)?

为什么标识其发起者的请求未通过fetch和XMLHttpRequest标准强制执行?是否应该使用其他东西进行决策(例如,客户提供他们期望的内容类型)?

2 个解决方案

#1


5  

Check out this issue on the Github's polyfill repository, specially this comment.

在Github的polyfill存储库中查看此问题,特别是此评论。

Since the X-Requested-With header is not an standard, they are using a wrapper that provides some of the missing behavior.

由于X-Requested-With标头不是标准,因此它们使用包装器来提供一些缺少的行为。

If you need more guidance, check this lines of the wrapper code:

如果您需要更多指导,请检查包装代码的这一行:

function headers(options) {
  options = options || {}
  options.headers = options.headers || {}
  options.headers['X-Requested-With'] = 'XMLHttpRequest'
  return options
}

#2


3  

Why is a request identifying its initiator not enforced through standards

为什么标识其发起者的请求未通过标准强制执行

Because it shouldn't matter.

因为没关系。

HTTP clients ask for the thing they want to get. The server should give it to them.

HTTP客户端要求他们想要的东西。服务器应该给它们。

Giving clients something different based on what the client is tends to cause more problems than it solves. User-Agent sniffing being a prime example.

根据客户的需求为客户提供不同的东西往往会导致比解决更多的问题。用户代理嗅探是一个很好的例子。

Should something else be used for decision-making (e.g. clients providing the content-type they expect in response)?

是否应该使用其他东西进行决策(例如,客户提供他们期望的内容类型)?

Yes.

是。

The Accept header is specifically provided for allowing a client to specify which format they would prefer the data in when an HTTP resource is available in multiple formats.

Accept标头专门用于允许客户端在多种格式的HTTP资源可用时指定他们更喜欢哪种格式的数据。


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
© 2014-2018 ITdaan.com 粤ICP备14056181号