Chrome Warn:
i try to send a http request to extern Rest API but i steel have an use with the CORS Policy.
i deactivated the Chrome CORS Policy in Chrome, in this window everything working fine but in the Normal Chrome Window i become this Error:
Access to XMLHttpRequest at 'https:///search/fi' from origin 'null' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The 'Access-Control-Allow-Origin' header has a value 'https://*.de' that is not equal to the supplied origin.
hier is My Code:
<body>
<h1>Tes Request</h1>
<script>
window.addEventListener("load", () => test(), false);
async function test() {
return new Promise(
resolve => {
var req = new XMLHttpRequest();
req.open('POST','https://Test/v1.0/', true);
req.setRequestHeader("Authorization", "Basic " + btoa('Test'+":"+'Test'));
req.setRequestHeader('Accept', 'application/json');
req.setRequestHeader('Content-type', 'application/ecmascript');
req.setRequestHeader('Access-Control-Allow-Methods','*');
req.setRequestHeader('Access-Control-Allow-Origin', 'https://Test');
req.onreadystatechange = function() {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
var results = JSON.parse(this.response).value;
resolve(results);
}
else {
alert(Error);
}
}
};
var data = `
{
"search": {
"firma": {
"ustid": "Test"
}
},
"config": {
"page":1
}
}
`; req.withCredentials = true;
req.send(data);
});
}
</script>
</body>
Access-Control-Allow-Origin: https://app.neugeschaeft.de
Access-Control-Allow-Headers: Content-Type, Authorization, Set-Cookie, Cache-Control
Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS
Access-Control-Allow-Credentials: true
Can you check the browser's debug console and inspect the Network activity to see your request. Check that you have the origin in the request set to whatever the origin needs to be. The response needs to then contain Access-Control-Allow-Origin indicating that the origin is allowed to access the resource.