我知道这个qquestion问题已经问了好几次了,但是都没有解决我的问题。我正在使用axios从服务器获取令牌(http://dev.site.com:82/token)。我在本地主机上工作。我也提供了标题,但仍然出现错误No 'Access-Control-Allow-Origin' header is present
。是我的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);
});
};
}
我有没有做任何错误或错过了代码中的某些内容?
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;
}
上面是我的nginx配置。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句