javascript - Unable to fetch POST without no-cors in header -


on making request that:

return fetch(             'http://localhost:8000/login',             {   method: 'post',                 headers: new headers(                    {"content-type": "application/json",                     "accept":"application/json"}                 ),                  body: json.stringify(                    {'name': 'tom', 'password': 'soyer'}                 )              }            ).then( response => { console.log(response);})             .catch(err => console.log(err)) 

request running method options instead post. on adding mode: 'no-cors' request become post:

return fetch(             'http://localhost:8000/login',             {   method: 'post',                 mode: 'no-cors',                 headers: new headers(                    {"content-type": "application/json",                     "accept":"application/json"}                 ),                 body: json.stringify(                    {'name': 'tom', 'password': 'soyer'}                 )              }            ).then( response => { console.log(response);})             .catch(err => console.log(err)) 

but response not ok (even if network response status 200): {type: "opaque", url: "", status: 0, ok: false, statustext: ""…} suppose because

the allowed values content-type header are: application/x-www-form-urlencoded multipart/form-data text/plain

described here https://developer.mozilla.org/en-us/docs/web/http/access_control_cors

is way bring live post json data fetch?

the custom content-type header you're sending causes request preflighted, means options request, containing metadata post request dispatched, sent before actual post request.

your server needs prepared deal options request. haven't specified server written in, express example, can register middleware intercepts 'options' requests, sets access-control-allow-origin: * , access-control-allow-headers: content-type headers, , responds 200.

if possible make request using 'content-type': 'text/plain' header, solve problem. alternatively use bypasses xhr entirely, jsonp.