gpt4 book ai didi

javascript - React JS 在使用 Axios 的 XMLHttpRequest 中出错

转载 作者:行者123 更新时间:2023-12-05 07:38:25 24 4
gpt4 key购买 nike

我在前端使用 REACT-JS 框架:

这是我从REDUX-REACT

调用的 Action
export function UserLogin(values) {
var headers = {
'Access-Control-Allow-Origin': '*',
'Accept': 'application/json',
'Content-Type': 'application/json'
}

const request = axios.post('http://localhost:8000/login', headers, values).then(function(response){
/*() => {callback();}*/
console.log(response);
})
.catch((error) => {
// Error
if (error.response) {
console.log(error.response.data);

}
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);
}
});
return {
type: LOGIN_REQUEST_SUCCESS,
payload: request
};
}

出于测试目的,我从其他 localhost:8000 得到答案。

我收到以下错误:

Failed to load http://localhost:8000/login: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access.

控制台日志来自 error.request

最佳答案

您应该在服务器的响应 header 中设置 'Access-Control-Allow-Origin': '*',

更多详情,查看this answer .

关于javascript - React JS 在使用 Axios 的 XMLHttpRequest 中出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47910388/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com