小编Mi *_* La的帖子

获取错误请求的资源上没有"Access-Control-Allow-Origin"标头

我正在使用fetch API从其他API获取数据这是我的代码:

var result = fetch(ip, {
        method: 'get',
    }).then(response => response.json())
      .then(data => {
        country = data.country_name;
        let lat = data.latitude;
        let lon = data.longitude;                       
        //fetch weather api with the user country
        return fetch(`https://api.darksky.net/forecast/efc76f378da9a5bd8bb366a91ec6f550/${lat},${lon}`);
    })
    .then(response => response.json())
    .then(data => {
        // access the data of the weather api
        console.log(JSON.stringify(data))
    })
    .catch(error => console.log('Request failed', error));
Run Code Online (Sandbox Code Playgroud)

但我在控制台中遇到错误:

Fetch API cannot load https://api.darksky.net/forecast/efc76f378da9a5bd8bb366a91ec6f550/52.3824,4.8995. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. If …
Run Code Online (Sandbox Code Playgroud)

javascript fetch fetch-api

8
推荐指数
2
解决办法
1万
查看次数

标签 统计

fetch ×1

fetch-api ×1

javascript ×1