Adding mode:'no-cors'
to the request header ensures that the response will not be available in the response
When adding a "non-standard" header, the line 'access-control-allow-origin'
will cause an OPTIONS pre-check request, which your server must correctly process so that the POST request is even sent
You are also doing the wrong fetch
... fetch
returns a "promise" for a Response
object that has promise makers for json
, text
, etc., Depending on the type of content ...
In short, if your server side handles CORS correctly (which from your comment suggests this is the case), then the following should work
function send(){ var myVar = {"id" : 1}; console.log("tuleb siia", document.getElementById('saada').value); fetch("http://localhost:3000", { method: "POST", headers: { "Content-Type": "text/plain" }, body: JSON.stringify(myVar) }).then(function(response) { return response.json(); }).then(function(muutuja){ document.getElementById('väljund').innerHTML = JSON.stringify(muutuja); }); }
however, since your code is not really interested in JSON (it's still a string object) - it's easier to do
function send(){ var myVar = {"id" : 1}; console.log("tuleb siia", document.getElementById('saada').value); fetch("http://localhost:3000", { method: "POST", headers: { "Content-Type": "text/plain" }, body: JSON.stringify(myVar) }).then(function(response) { return response.text(); }).then(function(muutuja){ document.getElementById('väljund').innerHTML = muutuja; }); }
source share