【问题标题】:CORS - Status 200 but error in Chrome devtools consoleCORS - 状态 200 但 Chrome devtools 控制台中的错误
【发布时间】:2018-05-01 11:22:45
【问题描述】:

我遇到了 CORS 问题,问题是我的代码已执行(status 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,

【问题讨论】:

    标签: javascript xmlhttprequest cors serverless serverless-framework-offline


    【解决方案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": "*"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-25
      • 2016-02-08
      • 2013-05-23
      • 2022-10-25
      • 2021-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多