1
async handleSubmit(event) {
    event.preventDefault();
    console.log("test");
    const requestOptions = {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({
        val1: "val1",
        val2: 2
      })
    };

    const response = await fetch(
      myURL,
      requestOptions
    );
    if (response.status >= 200 && response.status <= 299) {
      const jsonResponse = await response.json();
      console.log(jsonResponse);
    } else {
      console.log(response.status, response.statusText);
    }
}

I wrote this handleSubmit function for a form that needs to send an API request and get back some data. However, it gives the error Typeerror failed to fetch every time with no further explanation or messages. I tried error handling but still no output at all from the fetch part. What could be the reason?

Mali. G
  • 41
  • 1
  • 5
  • have you missed async before function name ? – DEEPAK Jun 10 '20 at 14:44
  • @DEEPAK No, I didn't, code runs with no problems, except the fetch part – Mali. G Jun 10 '20 at 14:51
  • check your endpoint, try to paste it in your browser and see whether it really shows sth – tcf01 Jun 10 '20 at 14:53
  • @tsecheukfung01 Endpoint works fine with Postman with the raw body in JSON format – Mali. G Jun 10 '20 at 15:10
  • @Mali.G Check if this can help: https://stackoverflow.com/questions/49343024/getting-typeerror-failed-to-fetch-when-the-request-hasnt-actually-failed – tcf01 Jun 11 '20 at 00:56
  • Does this answer your question? [Getting "TypeError: failed to fetch" when the request hasn't actually failed](https://stackoverflow.com/questions/49343024/getting-typeerror-failed-to-fetch-when-the-request-hasnt-actually-failed) – Michael Freidgeim Feb 03 '22 at 20:34

0 Answers0