Allow Access-Control-Allow-Origin header using HTML5 fetch API

前端 未结 6 1120
广开言路
广开言路 2020-12-08 01:57

I am using HTML5 fetch API.

var request = new Request(\'https://davidwalsh.name/demo/arsenal.json\');

fetch(request).         


        
6条回答
  •  感动是毒
    2020-12-08 02:07

    I know this is an older post, but I found what worked for me to fix this error was using the IP address of my server instead of using the domain name within my fetch request. So for example:

    #(original) var request = new Request('https://davidwalsh.name/demo/arsenal.json');
    #use IP instead
    var request = new Request('https://0.0.0.0/demo/arsenal.json');
    
    fetch(request).then(function(response) {
        // Convert to JSON
        return response.json();
    }).then(function(j) {
        // Yay, `j` is a JavaScript object
        console.log(JSON.stringify(j));
    }).catch(function(error) {
        console.log('Request failed', error)
    });
    

提交回复
热议问题