我不知道为什么对流关闭了

时间:2019-08-30 10:03:31

标签: reactjs axios wordpress-rest-api

我正在将数据发布到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

1 个答案:

答案 0 :(得分:0)

更新的答案

我已经用邮递员测试了您的API,并且在邮递员中工作正常。见下图。 enter image description here

也将其集成到react应用程序中。但是实际的问题是 CORS 。因此,您的服务器应启用跨源请求。

enter image description here

旧答案

您可以在标题中添加以下代码

 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;
  }
})