Search code examples
reactjsaxioswordpress-rest-api

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


Solution

  • Updated Answer

    I have tested your API with postman and it's working fine in postman. See below image. enter image description here

    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.

    enter image description here

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