添加
mode:'no-cors'到请求标头可确保在响应中没有可用的响应
添加“非标准”标头后,该行将
'access-control-allow-origin'触发OPTIONS预检请求,您的服务器必须正确处理该预检请求,才能发送POST请求
你也做
fetch不对......
fetch返回了一个“承诺”
Response这对于承诺的创造者对象
json,
text等等。根据内容类型…
简而言之,如果您的服务器端能够正确处理CORS(从您的注释中可以看出来),则应该可以进行以下操作
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); });}但是,由于您的代码对JSON并没有真正的兴趣(毕竟它将对象字符串化),因此操作起来更简单
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; });}


