0

I am using axios post method, but seems my backend can not get the data, not sure if it's because of the data format? Couse when I test backend code with pure javascript fetch method, it works fine. Not sure how to fix it? Thank you for any suggestion.

axios Dev tool screenshot(backend can not get data): enter image description here

Java script fetch Dev tool screenshot(works fine) enter image description here

React js code:

export default class PersonList extends React.Component {
  constructor () {
    super();
    this.state = {
    doctorid: '',
    plandate: '',
    starttime: '',
    endtime: '',
  };
  this.handleChange = this.handleChange.bind(this);
}
handleChange (evt, field) {
  // check it out: we get the evt.target.name (which will be either "email" or "password")
  // and use it to target the key on our `state` object with the same name, using bracket syntax
  
  this.setState({ [field]: evt.target.value });
}

  handleSubmit = event => {
    event.preventDefault();

    const userObject = {
      doctorid: this.state.doctorid,
      plandate: this.state.plandate,
      starttime: this.state.starttime,
      endtime: this.state.endtime
    }
  
    axios.post(`http://localhost:80/reacttest/src/api/api?action=apptPlan`,userObject, {
      headers: { 
         'Content-type': 'application/json'
      }
    })

      .then(res => {
        console.log(res);
        console.log(res.data);
      }).catch((error) => {
        console.log(error)
    });
    // this.setState({ doctorid: '', plandate: '', starttime: '', endtime:''})
    
  }

  render() {
    return (
      <div>
        <form onSubmit={this.handleSubmit}>
          <label>
            <h5>Person doctorid:</h5>
            <input type="text" name="doctorid" onChange={(event)=>this.handleChange(event, "doctorid")} />
            <h5>plandate</h5>
            <input type="text" name="plandate" onChange={(event)=>this.handleChange(event, "plandate")} />
            <h5>starttime</h5>
            <input type="text" name="starttime" onChange={(event)=>this.handleChange(event, "starttime")} />
            <h5>endtime</h5>
            <input type="text" name="endtime" onChange={(event)=>this.handleChange(event, "endtime")}/>
          </label>
          <button type="submit">Add</button>
        </form>
      </div>
    )
  }
}

3
  • How do you parse it on the backend? You don't need to stringify the data before you send it with axios, you can just pass in the object Commented Oct 23, 2020 at 2:11
  • Hi, I used PHP REST API in the backend, and the format is json: header('Content-Type: application/json'); Commented Oct 23, 2020 at 2:15
  • I just update the code, and delete the stringfy part before send it with axios, but still have same problem. Commented Oct 23, 2020 at 2:17

1 Answer 1

1

Is because with Axios you are sending the data in JSON format and with fetch is sent in Form Data format, to send it with Axios in Form Data format you need to implement like this:

export default class PersonList extends React.Component {
  constructor () {
    super();
    this.state = {
    doctorid: '',
    plandate: '',
    starttime: '',
    endtime: '',
  };
  this.handleChange = this.handleChange.bind(this);
}

handleChange (evt, field) {
  // check it out: we get the evt.target.name (which will be either "email" or "password")
  // and use it to target the key on our `state` object with the same name, using bracket syntax
  
  this.setState({ [field]: evt.target.value });
}

  handleSubmit = event => {
    event.preventDefault();

    const userObject = new FormData();

    userObject.append('doctorid', this.state.doctorid);
    userObject.append('plandate', this.state.plandate);
    userObject.append('starttime', this.state.starttime);
    userObject.append('endtime', this.state.endtime);
  
    axios.post(`http://localhost:80/reacttest/src/api/api?action=apptPlan`,userObject, {
      headers: { 
         'Content-Type': 'multipart/form-data'
      }
    })

      .then(res => {
        console.log(res);
        console.log(res.data);
      }).catch((error) => {
        console.log(error)
    });
    // this.setState({ doctorid: '', plandate: '', starttime: '', endtime:''})
    
  }

  render() {
    return (
      <div>
        <form onSubmit={this.handleSubmit}>
          <label>
            <h5>Person doctorid:</h5>
            <input type="text" name="doctorid" onChange={(event)=>this.handleChange(event, "doctorid")} />
            <h5>plandate</h5>
            <input type="text" name="plandate" onChange={(event)=>this.handleChange(event, "plandate")} />
            <h5>starttime</h5>
            <input type="text" name="starttime" onChange={(event)=>this.handleChange(event, "starttime")} />
            <h5>endtime</h5>
            <input type="text" name="endtime" onChange={(event)=>this.handleChange(event, "endtime")}/>
          </label>
          <button type="submit">Add</button>
        </form>
      </div>
    )
  }
}

Or if you want to receive in your backend in JSON format just add the same header and not use JSON.stringify function just send the object in the Axios function.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.