【问题标题】:Will a preflight request be triggered if the request is same-origin but does not follow the header guidelines?如果请求是同源但不遵循标头指南,是否会触发预检请求?
【发布时间】:2021-04-19 06:42:24
【问题描述】:

发件人:https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#preflighted_requests

以下是一个将被预检的请求示例:

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>');     

上面的示例创建了一个 XML 正文以与 POST 请求一起发送。 此外,还设置了非标准 HTTP X-PINGOTHER 请求标头。这样的 标头不是 HTTP/1.1 的一部分,但通常对 Web 有用 应用程序。由于请求使用了Content-Type application/xml,并且由于设置了自定义标头,因此此请求是 预检。

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

【问题讨论】:

    标签: javascript html browser cors


    【解决方案1】:

    不,预检请求仅适用于跨站点请求。如果您查看 MDN 文章的开头,则会有一张图片明确表示“Same origin-requets (always allowed)”作为示例。

    如果在暗模式下运行 SO,下面的图像不是很明显,如果是这样,请检查下面 url 上文章中的图像。

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

    【讨论】:

      猜你喜欢
      • 2020-02-21
      • 2017-11-03
      • 2021-09-23
      • 1970-01-01
      • 2020-05-06
      • 2011-07-03
      • 2017-03-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多