Skip to content
Advertisement

How to post a form with fetching?

I am trying to send a post with fetch an API.

My fetch is working because it returns 200 and sends the request but it sends an empty form.

It cannot take the values in the form input. Where is my mistake and how can I fix it?

JavaScript

Advertisement

Answer

[Update]:

You can either remove the application/json header and let browser post the form data implicitly using the multipart/form-data content-type :

JavaScript

As @Quentin pointed out, we cannot serialise FormData using JSON.stringify so it’s better to use explicit function to do it for you:

JavaScript

More details : https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement