13

In my React app when I submit a form to create a new user I create a request to the server using fetch.

The code is this one:

fetch('http://0.0.0.0:3000/users', {
  method: 'POST',
  mode: 'no-cors',
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify(data)
}).then(response => console.log(response))

When I check the entry in the Network tab under the Developer Tools I see that the Content-Type is text/plain but I do not understand why if I set it as application/json.

I have tried using the option Edit and send in the request and replace text/plain to application/json but after sending it it is sent as text/plain.

abaracedo
  • 1,404
  • 5
  • 26
  • 45
  • If you've set `no-cors` because you were getting _"No Access-Control-Allow-Origin header is present..."_ errors, that is not a solution – Phil Jan 11 '19 at 01:31

1 Answers1

12

The mode:"no-cors" option is the culprit.

Remove that option and it should work

To fix it pass a new headers object

headers: new Headers({'content-type': 'application/json'})
varoons
  • 3,807
  • 1
  • 16
  • 20