提问者:小点点

在 JavaScript 中获取 API 中的输出意外结束 [重复]


我在JavaScript中使用fetch方法时遇到此错误。fetch问题:输入意外结束

     let myinit={
    method:'GET',
    mode:'no-cors',
    headers: {
        'Content-Type': 'application/json',
        'Accept': 'application/json',
        'Connection': 'keep-alive',
        'Accept-Encoding' : 'gzip, deflate, br',
    },
    cache:'default'
};

let myRequest =new Request('https://opendata.ecdc.europa.eu/covid19/casedistribution/json/',myinit);
fetch(myRequest)
    .then(function(response){
        return response.json();
    })
    .then(function(data){
        console.log(data);
    })
    .catch(function (err) {
        console.log('Fetch problem: ', err.message);
    });

共1个答案

匿名用户

试试这个:

let myinit={
    method:'GET',
    // mode:'no-cors',
    headers: {
        'Content-Type': 'application/json',
        'Accept': 'application/json',
        'Connection': 'keep-alive',
        'Accept-Encoding' : 'gzip, deflate',
    },
    cache:'default'
};
let myRequest =new Request('https://cors-anywhere.herokuapp.com/https://opendata.ecdc.europa.eu/covid19/casedistribution/json/',myinit);
fetch(myRequest)
    .then(function(response){
        return response.json();
    })
    .then(function(data){
        console.log(data);
    })
    .catch(function (err) {
        console.log('Fetch problem: ', err.message);
    });