CORS - 状态 200,但 Chrome 开发者工具控制台中出现错误

pir*_*max 5 javascript xmlhttprequest cors serverless serverless-framework-offline

我遇到了 CORS 问题,问题是我的代码已执行(状态 200),但 Google Chrome 开发者控制台中出现错误。

我的代码:

function callUrl(url) {
    var xmlhttp = null;

    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    if (xmlhttp !== null) {
        xmlhttp.open("POST", url, true);
        xmlhttp.withCredentials = true;
        xmlhttp.send(JSON.stringify({
        }));
    } else {
        console.error("Your browser does not support XMLHTTP.");
    }
}

callUrl('https://www.website.com/tracking?etc...');
Run Code Online (Sandbox Code Playgroud)

XMLHttpRequest 无法加载https://www.website.com/tracking?。当请求的凭据模式为“include”时,响应中“Access-Control-Allow-Origin”标头的值不能是通配符“*”。因此,不允许访问来源“ http://localhost:8888 ”。XMLHttpRequest 发起的请求的凭据模式由 withCredentials 属性控制。

服务器配置:

"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": true,
Run Code Online (Sandbox Code Playgroud)

shu*_*304 1

在 Firefox 中,即使它正在运行并且状态为 200,您也会收到此错误。

您可以在https://developer.mozilla.org/en-US/docs/Web/HTTP/Server-Side_Access_Control中阅读相关内容。

如果使用 xmlhttp.withCredentials = true; 您需要指定 PHP 代码中允许的特定来源。如果任何来源可以调用您的 API ...删除此属性,以便您将能够使用 "Access-Control-Allow-Origin": "*"