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

前端 未结 4 1034
无人共我
无人共我 2020-11-30 04:05

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 ca

相关标签:
4条回答
  • 2020-11-30 04:22

    See mozilla.org's write-up on how CORS works.

    You'll need your server to send back the proper response headers, something like:

    Access-Control-Allow-Origin: http://foo.example
    Access-Control-Allow-Methods: POST, PUT, GET, OPTIONS
    Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization
    

    Bear in mind you can use "*" for Access-Control-Allow-Origin that will only work if you're trying to pass Authentication data. In that case, you need to explicitly list the origin domains you want to allow. To allow multiple domains, see this post

    0 讨论(0)
  • 2020-11-30 04:26

    you can use solutions without adding "Access-Control-Allow-Origin": "*", if your server is already using Proxy gateway this issue will not happen because the front and backend will be route in the same IP and port in client side but for development, you need one of this three solution if you don't need extra code 1- simulate the real environment by using a proxy server and configure the front and backend in the same port

    2- if you using Chrome you can use the extension called Allow-Control-Allow-Origin: * it will help you to avoid this problem

    3- you can use the code but some browsers versions may not support that so try to use one of the previous solutions

    the best solution is using a proxy like ngnix its easy to configure and it will simulate the real situation of the production deployment

    0 讨论(0)
  • 2020-11-30 04:31

    In my case, the problem was the protocol. I was trying to call a script url with http instead of https.

    0 讨论(0)
  • 2020-11-30 04:36

    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;
        });
    }
    
    0 讨论(0)
提交回复
热议问题