我正在尝试将heroku
应用中的数据提取到Redux
,但我认为我遇到了CORS
问题。我已经玩了一段时间的“标题”,但仍然无法弄明白。
这是确切的错误:
无法加载https://name-app.herokuapp.com/users:响应 预检请求未通过访问控制检查:否 请求中存在“Access-Control-Allow-Origin”标头 资源。因此不允许来源“http://localhost:3000” 访问。如果不透明的响应满足您的需求,请设置请求 模式为'no-cors'以获取禁用CORS的资源。
这是我的代码:
export function fetchMicros() {
return dispatch => {
const url = "https://name-app.herokuapp.com/users";
return fetch(url, {
method: 'GET',
mode: 'cors',
headers: { 'Content-Type': 'text' },
})
.then(handleErrors)
.then(res => res.json())
.then(micros => {
dispatch(fetchVitamins(micros));
}
)};
};
我也尝试过使用"no-cors"
,当我这样做时,我的错误函数handleErrors会抛出错误
function handleErrors(response) {
if (!response.ok) {
throw Error(response.statusText);
}
return response;
}