我正在使用React和fetch API向用户身份验证后端发出POST请求。我可以使用Postman完成下面的POST请求,并得到正确的JWT,但奇怪的是,每当我在React中使用以下代码时,POST请求以某种方式作为get请求到达服务器。
React中的代码:
return this.fetch('http://fakeURL.com/auth', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
"email": "email@email.com",
"password": "password",
})
}).then(res => {
this.setToken(res.token);
return Promise.resolve(res);
})
日志显示(首先是飞行前请求):
Request URL: http://fakeURL.com/auth
Request Method: OPTIONS
Status Code: 200 OK
Referrer Policy: no-referrer-when-downgrade
和实际的请求:
Request URL: http://fakeURL.com/auth
Request Method: GET
Status Code: 405 METHOD NOT ALLOWED
Referrer Policy: no-referrer-when-downgrade
我尝试过的东西:
在本地和在亚马逊网络服务S3存储桶中运行应用程序的
这太令人困惑了--是什么可能导致我们的POST请求作为GET请求发出呢?我觉得我们已经排除了React中发生的奇怪事情之外的所有可能的原因。谢谢你的帮助!
发布于 2018-06-13 21:13:52
npm install --保存axios
在身份验证页面上:从' axios‘导入axios
修改你的帖子,而不是使用fetch:
login(emailValue, passwordValue) {
return axios({
url: `yourAuthURL`,
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
data: {
"email": emailValue,
"password": passwordValue,
}
}).then(res => {
console.log(res);
})
}
https://stackoverflow.com/questions/50825537
复制相似问题