2017-05-06 8 views
4

上に存在している私は、これは私のコードである他の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)); 

を私はエラーに直面コンソールで:

return fetch(`https://api.darksky.net/forecast/efc76f378da9a5bd8bb366a91ec6f550/${lat},${lon}`, { 
    mode: 'no-cors', 
    header: { 
    'Access-Control-Allow-Origin':'*', 
    } 
}); 

エラーは消えますが、console.log(JSON.stringify(data))行います

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 an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled. 

は私がフェッチ秒間ヘッダーを設定しましたコンソールで何もログに記録せず、フェッチは値を返しません。 私のコードで何が問題になっていますか?

+0

http://stackoverflow.com/q/20035101/2037335 – Jude

答えて

3

このAPIの所有者である場合、問題はJavaScriptコードにありません。APIにあります。サーバーはクロスオリジン要求をサポートしていません。'Access-Control-Allow-Origin 'のヘッダーを許可された起点(*は任意の起点を許可する)で応答します。 jsonpリクエストが有効な場合があります。

注:この問題は、要求がブラウザから生成された場合にのみ発生します。

関連する問題