我正在尝试对后端服务器进行post调用,但是我总是遇到这个错误: TypeError: Failed to fetch
我已经检查了代码很多次,但似乎没有找到问题所在。代码如下:
async doLogin() {
if(!this.state.email || !this.state.password) {
return
}
this.setState({
buttonDisabled : true
})
try {
let res = await fetch('/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
email: this.state.email,
password: this.state.password
})
})
console.log(res)
let result = await res.json()
console.log(result)
if(result && result.success) {
UserStores.isLoggedIn = true
UserStores.email = result.email
alert(result.msg)
} else if(result && result.success === false) {
this.resetForm()
alert(result.msg)
}
} catch(e) {
console.log('doLogin error: ', e)
this.resetForm()
}
}
这是一个响应负载示例:
{
"success": true,
"email": "mfultz956@gmail.com",
"msg": "Login Verified!"
}
发布于 2020-07-30 13:55:34
将其更改为:
let res = await fetch('http://localhost:your_api_server_port/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
email: this.state.email,
password: this.state.password
})
})
https://stackoverflow.com/questions/63166820
复制相似问题