CORS - 状态 200 但 Chrome devtools 控制台出错
CORS - Status 200 but error in Chrome devtools console
我遇到 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 cannot load https://www.website.com/tracking?. The
value of the 'Access-Control-Allow-Origin' header in the response must
not be the wildcard '*' when the request's credentials mode is
'include'. Origin 'http://localhost:8888' is therefore not allowed
access. The credentials mode of requests initiated by the
XMLHttpRequest is controlled by the withCredentials attribute.
服务器配置:
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": true,
在 Firefox 中,您将收到此错误...即使它正在运行并获得状态 200。
您可以在 https://developer.mozilla.org/en-US/docs/Web/HTTP/Server-Side_Access_Control 中阅读相关信息。
如果使用 xmlhttp.withCredentials = true;您需要指定 PHP 代码中允许的特定来源。如果有任何来源可以调用您的 API ... 删除此属性,这样您就可以使用 "Access-Control-Allow-Origin": "*"
我遇到 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 cannot load https://www.website.com/tracking?. The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. Origin 'http://localhost:8888' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
服务器配置:
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": true,
在 Firefox 中,您将收到此错误...即使它正在运行并获得状态 200。
您可以在 https://developer.mozilla.org/en-US/docs/Web/HTTP/Server-Side_Access_Control 中阅读相关信息。
如果使用 xmlhttp.withCredentials = true;您需要指定 PHP 代码中允许的特定来源。如果有任何来源可以调用您的 API ... 删除此属性,这样您就可以使用 "Access-Control-Allow-Origin": "*"