简体   繁体   English

如果请求是同源但不遵循 header 指南,是否会触发预检请求?

[英]Will a preflight request be triggered if the request is same-origin but does not follow the header guidelines?

From: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#preflighted_requests来自: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#preflighted_requests

The following is an example of a request that will be preflighted:以下是将进行预检的请求示例:

 const xhr = new XMLHttpRequest(); xhr.open('POST', 'https://bar.other/resources/post-here/'); xhr.setRequestHeader('X-PINGOTHER', 'pingpong'); xhr.setRequestHeader('Content-Type', 'application/xml'); xhr.onreadystatechange = handler; xhr.send('<person><name>Arun</name></person>');

The example above creates an XML body to send with the POST request.上面的示例创建了一个 XML 正文以与POST请求一起发送。 Also, a non-standard HTTP X-PINGOTHER request header is set.此外,还设置了一个非标准的 HTTP X-PINGOTHER请求 header。 Such headers are not part of HTTP/1.1, but are generally useful to web applications.此类标头不是 HTTP/1.1 的一部分,但通常对 web 应用程序有用。 Since the request uses a Content-Type of application/xml , and since a custom header is set, this request is preflighted.由于该请求使用application/xmlContent-Type ,并且设置了自定义 header,因此该请求是预检的。

Will a preflight request be triggered if the request is same-origin but does not follow the header guidelines?如果请求是同源但不遵循 header 指南,是否会触发预检请求?

No, preflight requests are only done for cross-site requests.不,预检请求仅针对跨站点请求。 If you look at the beginning of the MDN article there is an image that explicitly says "Same origin-requets (always allowed)" as an example.如果您查看 MDN 文章的开头,有一张图片明确表示“同源请求(始终允许)”作为示例。

The below image isn't very visible if running SO in dark mode, if so, check the image in the article on the url below.如果在黑暗模式下运行 SO,下图不是很明显,如果是这样,请查看下面 url 上的文章中的图像。

文章中的示例图片

https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM