reactjs - I couldn't figure out why is the conncection is closed
问题描述
I'm posting data to the wordpress api but I got this problem of closed connection .. first time it was working but I dont know what happened
handleSignIn = () => {
const post = {
"email" : this.state.email,
"password " : this.password
}
axios.post('http://api.piri.ai:3000/v1/public/login' , post)
.then(res => {this.setState({status: res.data.status})
//window.location= '/wp';
console.log(res.data)
})
.catch(e => {console.log(e.message) ; this.setState({status: false})});
}
in the console log, I got this error
Network Error xhr.js:166 OPTIONS https://api.piri.ai:3000/v1/public/login net::ERR_CONNECTION_CLOSED
解决方案
Updated Answer
I have tested your API with postman and it's working fine in postman. See below image.
Also integrate it in react application as well. But the actual issue is CORS. So for that your server should enable the cross origin requests.
Old Answer
Can you add below code in header
headers:{
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json'
}
You can handle network error in catch part like below.
catch(error => {
if (!error.response) {
// network error
this.errorStatus = 'Error: Network Error';
} else {
this.errorStatus = error.response.data.message;
}
})
推荐阅读
- angular5 - angular 5 使用 HttpClient 发送请求标头和接收响应标头
- android - Android:Recyclerview onBindViewHolder 和 Snaphelper
- r - 在两列上聚合
- rest - 将分页信息传递给 RestController 的最佳方法是什么?
- python - Django-Tteebeard在django表单中使用treadbeard字段表单
- jquery - laravel csrf in javascript return 页面由于不活动而过期。请刷新并重试
- php - 如何在 PHP 中取消设置数组的子值?
- java - Java jdbc程序错误在运行程序时进入程序找不到合适的类
- amazon-s3 - 相当于 Amazon STS 的 Google Cloud
- c# - C# 迁移触发器更新