我正在将数据发布到wordpress api,但是我遇到了关闭连接的问题..第一次它起作用了,但是我不知道发生了什么
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})});
}
在控制台日志中,出现此错误
网络错误 xhr.js:166选项https://api.piri.ai:3000/v1/public/login净:: ERR_CONNECTION_CLOSED
答案 0 :(得分:0)
我已经用邮递员测试了您的API,并且在邮递员中工作正常。见下图。
也将其集成到react应用程序中。但是实际的问题是 CORS 。因此,您的服务器应启用跨源请求。
您可以在标题中添加以下代码
headers:{
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json'
}
您可以在捕获部分中处理网络错误,如下所示。
catch(error => {
if (!error.response) {
// network error
this.errorStatus = 'Error: Network Error';
} else {
this.errorStatus = error.response.data.message;
}
})