CORS - 状态码200但Chrome开发者工具控制台显示错误

5

我遇到了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...');

XMLHttpRequest无法加载https://www.website.com/tracking。当请求的凭据模式为“包括”时,响应中的“Access-Control-Allow-Origin”标头的值不能是通配符“*”。因此,源'http://localhost:8888'不被允许访问。XMLHttpRequest发起的请求的凭据模式由withCredentials属性控制。 服务器配置:
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": true,
1个回答

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”:“*”。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接