How to send form data to REST api endpoint?

时间:2019-04-17 02:03:12

标签: reactjs rest api post

I have an endpoint which leads to a backend REST API form where you can add a new user. My function is sending a POST request because I get no error on my end, but whenever I go check my API and view users, it did add a new object but with null values. I tried to make sure my variables are called the same as the backend, but no luck.

handleSubimt function:

    handleSubmit(event) {
      event.preventDefault();

      const data = new FormData(event.target);

      fetch('http://localhost:8080/seniorproject/addUser', {
        method: 'POST',
        body: data,
      });

      console.log(data);
    }

Form component:

render() {
      return (
        <form onSubmit={this.handleSubmit}>
          <label htmlFor="First Name">Enter First Name</label>
          <input id="firstName" name="firstName" type="text" />

          <label htmlFor="Last Name">Enter your Last Name</label>
          <input id="lastName" name="lastName" type="lastName" />

          <label htmlFor="studentID">Enter your student id</label>
          <input id="studentID" name="studentID" type="text" />

          <label htmlFor="Email">Enter your email</label>
          <input id="email" name="email" type="text" />

          <label htmlFor="Password">Enter your password</label>
          <input id="password" name="password" type="text" />

          <label htmlFor="Avatar">Enter your avatar</label>
          <input id="avatar" name="avatar" type="text" />

          <button>Send data!</button>
        </form>
      );

0 个答案:

没有答案