• <bdo id='Kklwx'></bdo><ul id='Kklwx'></ul>
  • <legend id='Kklwx'><style id='Kklwx'><dir id='Kklwx'><q id='Kklwx'></q></dir></style></legend>
    <i id='Kklwx'><tr id='Kklwx'><dt id='Kklwx'><q id='Kklwx'><span id='Kklwx'><b id='Kklwx'><form id='Kklwx'><ins id='Kklwx'></ins><ul id='Kklwx'></ul><sub id='Kklwx'></sub></form><legend id='Kklwx'></legend><bdo id='Kklwx'><pre id='Kklwx'><center id='Kklwx'></center></pre></bdo></b><th id='Kklwx'></th></span></q></dt></tr></i><div id='Kklwx'><tfoot id='Kklwx'></tfoot><dl id='Kklwx'><fieldset id='Kklwx'></fieldset></dl></div>

      <tfoot id='Kklwx'></tfoot>

      <small id='Kklwx'></small><noframes id='Kklwx'>

        Uncaught (in promise) TypeError: Failed to fetch and Cors err

        时间:2023-09-03

          1. <legend id='v5K75'><style id='v5K75'><dir id='v5K75'><q id='v5K75'></q></dir></style></legend>
                <tbody id='v5K75'></tbody>
                <bdo id='v5K75'></bdo><ul id='v5K75'></ul>
              • <small id='v5K75'></small><noframes id='v5K75'>

                <tfoot id='v5K75'></tfoot>
                <i id='v5K75'><tr id='v5K75'><dt id='v5K75'><q id='v5K75'><span id='v5K75'><b id='v5K75'><form id='v5K75'><ins id='v5K75'></ins><ul id='v5K75'></ul><sub id='v5K75'></sub></form><legend id='v5K75'></legend><bdo id='v5K75'><pre id='v5K75'><center id='v5K75'></center></pre></bdo></b><th id='v5K75'></th></span></q></dt></tr></i><div id='v5K75'><tfoot id='v5K75'></tfoot><dl id='v5K75'><fieldset id='v5K75'></fieldset></dl></div>
                1. 本文介绍了Uncaught (in promise) TypeError: Failed to fetch and Cors error的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  having a problem with getting data back from database. I am trying my best to explain the problem.

                  1.If I leave "mode":"no-cors" inside the code below, then I can get data back from server with Postman, but not with from my own server. Thinking it has to be my client side error

                  1. When I remove "mode":"no-cors" then I am getting 2 errors: -Fetch API cannot load http://localhost:3000/. Request header field access-control-allow-origin is not allowed by Access-Control-Allow-Headers in preflight response. -Uncaught (in promise) TypeError: Failed to fetch

                  Quick Browsing suggested to put in the "mode":"no-cors" which fixed this error, but it does not feel right thing to do.

                  So I thought maybe somebody has a suggestion how to approach this problem.

                  Really hope I was clear enough, but pretty sure I am not giving clear explanation here :S

                  function send(){
                      var myVar = {"id" : 1};
                      console.log("tuleb siia", document.getElementById('saada').value);
                      fetch("http://localhost:3000", {
                          method: "POST",
                          headers: {
                              "Access-Control-Allow-Origin": "*",
                              "Content-Type": "text/plain"
                          },//"mode" : "no-cors",
                          body: JSON.stringify(myVar)
                          //body: {"id" : document.getElementById('saada').value}
                      }).then(function(muutuja){
                  
                          document.getElementById('väljund').innerHTML = JSON.stringify(muutuja);
                      });
                  }
                  

                  解决方案

                  Adding mode:'no-cors' to the request header guarantees that no response will be available in the response

                  Adding a "non standard" header, line 'access-control-allow-origin' will trigger a OPTIONS preflight request, which your server must handle correctly in order for the POST request to even be sent

                  You're also doing fetch wrong ... fetch returns a "promise" for a Response object which has promise creators for json, text, etc. depending on the content type...

                  In short, if your server side handles CORS correctly (which from your comment suggests it does) 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 isn't really interested in JSON (it stringifies the object after all) - it's simpler 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;
                      });
                  }
                  

                  这篇关于Uncaught (in promise) TypeError: Failed to fetch and Cors error的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:使用 file://在本地加载的来自移动 WebView 的 CORS 下一篇:如何阻止恶意代码欺骗“Origin"?标头利用COR

                  相关文章

                  最新文章

                    <tfoot id='5RYqh'></tfoot>

                    <small id='5RYqh'></small><noframes id='5RYqh'>

                      • <bdo id='5RYqh'></bdo><ul id='5RYqh'></ul>
                      <i id='5RYqh'><tr id='5RYqh'><dt id='5RYqh'><q id='5RYqh'><span id='5RYqh'><b id='5RYqh'><form id='5RYqh'><ins id='5RYqh'></ins><ul id='5RYqh'></ul><sub id='5RYqh'></sub></form><legend id='5RYqh'></legend><bdo id='5RYqh'><pre id='5RYqh'><center id='5RYqh'></center></pre></bdo></b><th id='5RYqh'></th></span></q></dt></tr></i><div id='5RYqh'><tfoot id='5RYqh'></tfoot><dl id='5RYqh'><fieldset id='5RYqh'></fieldset></dl></div>
                    1. <legend id='5RYqh'><style id='5RYqh'><dir id='5RYqh'><q id='5RYqh'></q></dir></style></legend>