missing token ‘x-auth’ in CORS header ‘Access-Control-Allow-Headers’ from CORS preflight channel
See original GitHub issuei want to get data with post method from axios package and xampp sever, i have an error when work with firefox :
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://127.0.0.1/aftab/inventory3/v1/repository/all. (Reason: missing token ‘x-auth’ in CORS header ‘Access-Control-Allow-Headers’ from CORS preflight channel).[Learn More] Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://127.0.0.1/aftab/inventory3/v1/repository/all. (Reason: CORS request did not succeed)
but when test it in chrome it work correctly and i don’t have any error ,
my axios request code is:
console.log(“iman”);
let page = 1;
let config =
{
‘Content-Type’: ‘application/x-www-form-urlencoded’,
‘x-auth’: localStorage.getItem(“token”),
‘Access-Control-Allow-Origin’: ‘*’
};
let data = {page: page};
if (localStorage.getItem(“token”)) {
await axios({
method: “post”,
url: “http://127.0.0.1/aftab/inventory3/v1/repository/all”,
data: data,
headers: config
}).then(function (response) {
console.log(response);
}).catch(function (error) {
if (error.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
console.log(error.response.data);
console.log(error.response.status);
console.log(error.response.headers);
} else if (error.request) {
// The request was made but no response was received
// error.request
is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
console.log(error.request);
} else {
// Something happened in setting up the request that triggered an Error
console.log(‘Error’, error.message);
}
console.log(error.config);
});
} else {
localStorage.removeItem(“token”);
// this.history.push(‘/log/in’);
}
in firefox console network, only option method was sent and after it we dont have any request but in the chrome it work correct and we have a post method request after option method
Issue Analytics
- State:
- Created 5 years ago
- Comments:10
Top GitHub Comments
@Kshopper93 Don’t set
Access-Control-Allow-Methods/Access-Control-Allow-Headers
to*
simply. In your case, use a certain value,Access-Control-Allow-Headers: Api-Key
. Refer MDN.@chinesedfan Dammm you saved my day 😄 Changed my Access-Control-Allow-Headers on server to->
<add name="Access-Control-Allow-Headers" value="*,Api-Key" />
and it works