0

Send a request to a server and need to send the header 'Content-Type': 'application/json'

all this is done with fetch, but because of crossdomain have to specify

mode: 'no-cors',

And in this mode, the browser defaults to - 'Content-Type': 'text/plain'

Actually, this is the problem, since 415 error is returned Here is the entire request code

realFetch(url {
        method: 'POST',
        mode: 'no-cors',
        cache: 'no-cache',
        credentials: "same-origin",
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify(dataUser)
      }).

Prompt how to solve this problem? How to force the browser to change headers using fetch?

Kobe
  • 6,226
  • 1
  • 14
  • 35

1 Answers1

1

You can't.

Setting no-cors tells fetch that you do not need CORS permission and so will not doing anything that needs permission.

Setting the Content-Type header to a value other than one of the three allowed by the HTML enctype attribute requires permission from the server using CORS.

Since you have told fetch not to check for permission, it ignores your instruction to do something normally forbidden.


This answer has more information about CORS and cross-origin requests.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335