admin管理员组

文章数量:1277910

I am new to JavaScript. How can I add mode: no-cors to my XMLHttpRequest in JavaScript?

For example, to this code:

var xhr = new XMLHttpRequest();
xhr.open("POST", yourUrl, true);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(JSON.stringify({
    value: value
}));

I am new to JavaScript. How can I add mode: no-cors to my XMLHttpRequest in JavaScript?

For example, to this code:

var xhr = new XMLHttpRequest();
xhr.open("POST", yourUrl, true);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(JSON.stringify({
    value: value
}));
Share Improve this question edited Oct 12, 2022 at 15:00 Yves M. 31k24 gold badges109 silver badges149 bronze badges asked Aug 16, 2021 at 10:01 разия Утегеноваразия Утегенова 831 gold badge1 silver badge9 bronze badges 1
  • Does this answer your question? stackoverflow./questions/14667512/… – Adil Bimzagh Commented Aug 16, 2021 at 10:05
Add a ment  | 

1 Answer 1

Reset to default 7

XMLHttpRequest does not support no-cors mode, only fetch does.

However, you are trying set a Content-Type of application/json which requires permission from CORS, so you can't use no-cors mode anyway.

If you used fetch and set no-cors mode then the instruction to set the Content-Type would be ignored.

Instead, configure yourUrl to grant permission, using CORS, to make the request with the JSON Content-Type.

本文标签: javascriptHow to add mode 39nocors39 to XMLHttpRequestStack Overflow