[英]No 'Access-Control-Allow-Origin' header is present on requested resource
I know this qquestion has asked several times but none has solved my problem.I am using axios for fetching the token from the server( http://dev.site.com:82/token ). 我知道这个问题已经问了好几次了,但是都没有解决我的问题。我正在使用axios从服务器上获取令牌( http://dev.site.com:82/token )。 I am working on localhost.
我在本地主机上工作。 I have supplied the header too but still i get an error of
No 'Access-Control-Allow-Origin' header is present
. 我也提供了标题,但仍然出现错误,提示
No 'Access-Control-Allow-Origin' header is present
。 Is this error being produced from my js code(axios) or it is a problem in server? 是从我的js代码(axios)产生此错误,还是服务器中存在问题?
const config = {
method: 'get',
url: `${API_URL}/token`,
headers: {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json',
},
};
export function getToken() {
return function (dispatch) {
console.log('action triggered atleast');
axios.request(config)
.then(response => {
console.log('response', response);
localStorage.setItem('token', response.data.token);
dispatch({ type: GET_TOKEN });
})
.catch(error => {
errorHandler(dispatch, error.response, TOKEN_ERROR);
});
};
}
Have i done any mistake or missed something in my code? 我有没有做任何错误或错过了代码中的某些内容?
UDPATE UDPATE
add_header 'Access-Control-Allow-Origin' "*";
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'X-CustomHeader,Accept,Authorization,Cache-Control,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Requested-With';
# required to be able to read Authorization header in frontend
add_header 'Access-Control-Expose-Headers' 'Authorization' ;
if ($request_method = 'OPTIONS') {
# Tell client that this pre-flight info is valid for 20 days
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 204;
}
Above is my nginx configuration. 上面是我的nginx配置。
The server need to send the back the CORS header not the request 服务器需要发回CORS标头,而不是请求
Take a look here how to enable cors: http://enable-cors.org/server.html 在这里看看如何启用cors: http ://enable-cors.org/server.html
If you don't have access to the server then maybe you want to try something like a cors proxy 如果您无权访问服务器,则可能要尝试使用诸如cors代理之类的东西
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.