尽管有适当的 Access-Control-Allow-* headers,但 CORS ajax 调用失败

CORS ajax call fails despite of the appropriate Access-Control-Allow-* headers

我正在执行从 third-party-hosted 脚本到我的端点的 ajax 调用。

预检调用在 Chrome 中看起来像这样:

GENERAL
Request URL: https://my_endpoints_url
Request Method: OPTIONS
Status Code: 200 
Remote Address: 21.188.37.117:443
Referrer Policy: origin-when-cross-origin

RESPONSE HEADERS
access-control-allow-headers: *
access-control-allow-methods: *
access-control-allow-origin: *
allow: OPTIONS, TRACE, GET, HEAD, POST
content-length: 0
date: Thu, 14 Jan 2021 15:45:17 GMT
public: OPTIONS, TRACE, GET, HEAD, POST
server: Microsoft-IIS/10.0
strict-transport-security: max-age=31536000
x-powered-by: ASP.NET

REQUEST HEADERS
:authority: my_endpoints_host
:method: OPTIONS
:path: my_endpoints_path
:scheme: https
accept: */*
accept-encoding: gzip, deflate, br
accept-language: fr,en-US;q=0.9,en;q=0.8,ca;q=0.7,es;q=0.6,pt;q=0.5
access-control-request-headers: authorization,content-type
access-control-request-method: POST
origin: https://c.cs160.visual.force.com
referer: https://c.cs160.visual.force.com/
sec-fetch-dest: empty
sec-fetch-mode: cors
sec-fetch-site: cross-site
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36

我将端点的服务器配置为允许响应 header 中可见的每个 header、方法和来源。尽管如此,Chrome 不断取消后续的 POST 请求。

我错过了什么?

编辑: 如果我从服务器响应中删除 access-control-allow-headers,Chrome 会在控制台中写入正确的错误消息“预检响应中的 Access-Control-Allow-Headers 不允许请求 header 字段 content-type。” POST 请求在网络选项卡中被标记为(失败)。添加access-control-allow-headers时:"*",没有报错,POST请求被标记为(已取消)

谢谢

解决方案与 CORS 完全无关,而是托管我的 JS 脚本的网站 (SalesForce)。

确实,SalesForce 在每次 ajax 调用后都会重新加载页面。 Chrome 检测到触发 ajax 调用的 DOM 节点已经消失,因此它明智地取消了请求。

我的解决方案是让我的 ajax 调用同步。

感谢@Quentin 和@sideshowbarker 的想法