Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
804 views
in Technique[技术] by (71.8m points)

reactjs - Form POST Error: 422 unprocessable Entity

I'm trying to post input values from a form, and fetch them in an existing JSON on submit in React.

The problem is I keep getting this error:

error: "Unprocessable Entity" message: "Request cannot be processed" statusCode: 422

Here is some of the relevant code (I didn't add the entire form, but the syntax is the same):

  async function onSubmit(data) {
    console.log(data);
    const enquiryInput = {
      name: data.name,
      email: data.email,
      checkIn: data.checkin,
      checkOut: data.checkout,
    };

    const url = BASE_URL + "enquiries";

    const options = {
      headers: headers,
      method: POST,
      body: JSON.stringify(enquiryInput),
    };

    console.log(options);

      await fetch(url, options);

    //history.push("/admin/hotels");
  }


  return (
    <Row className="form">

        <Form onSubmit={handleSubmit(onSubmit)} onReset={reset}>

          <Form.Group>
            <Form.Label htmlFor="name" className="form__label">
              {" "}
              <Person />
              Name
            </Form.Label>
            <Form.Control
              name="name"
              id="name"
              type="text"
              ref={register}
              className={`form__control ${errors.name ? "is-invalid" : ""}`}
              placeholder="Enter a name"
              required={true}
            />
            <div className="invalid-feedback">{errors.name?.message}</div>
          </Form.Group>
    
          <button className="btn" type="submit">
            Submit
          </button>
        </Form>
  );
}

export default Enquiry;```


Here is my header: 

```export const headers = {
  "Content-Type": "application/json",
  key: KEY,
};```
question from:https://stackoverflow.com/questions/66061038/form-post-error-422-unprocessable-entity

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...