请求的资源上不存在 'Access-Control-Allow-Origin' header
No 'Access-Control-Allow-Origin' header is present on requested resource
我知道这个问题已经问过好几次了,但是 none 已经解决了我的 problem.I 我正在使用 axios 从服务器获取令牌 (http://dev.site.com:82/token)。我在本地主机上工作。我也提供了 header,但我仍然收到 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配置。
服务器需要发回 CORS header 而不是请求
在这里查看如何启用 cors:http://enable-cors.org/server.html
如果您无法访问服务器,那么也许您想尝试 cors proxy
我知道这个问题已经问过好几次了,但是 none 已经解决了我的 problem.I 我正在使用 axios 从服务器获取令牌 (http://dev.site.com:82/token)。我在本地主机上工作。我也提供了 header,但我仍然收到 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配置。
服务器需要发回 CORS header 而不是请求
在这里查看如何启用 cors:http://enable-cors.org/server.html
如果您无法访问服务器,那么也许您想尝试 cors proxy