2016-09-20 11 views
0

私は、提供されたapiでopenweather.orgに接続するためのテストコードを試しています。

私は私のブラウザを使用してURLにアクセスする場合:

http://api.openweathermap.org/data/2.5/weather?id=2172797&APPID=35000cdad97645316c048563e4183021

その後、私は適切なJSONを取得: { "COORD":{ "経度":145.77、 "緯度": - 16.92 「雲」、「説明」、「雲が壊れている」、「アイコン」:「04n」}]、「ベース」:「駅」、「駅」、 "temp":289.26、 "pressure":1013、 "humidity":93、 "temp_min":289.26、 "temp_max":289.26}、 "wind":{"speed":1.61、 "deg" 1165}、 "rain":{"3h":0.03}、 "clouds":{"all":76}、 "dt":1474367584、 "sys":{"type":3、 "id":10843、 "message":0.1585、 "country": "AU"、 "sunrise":1474315673、 "sunset":1474359164}、 "id":2172797、 "name": "Cairns"、 "cod":200}

問題は、jqueryの$ .getJSONを使用しているときにデータが表示されないことです。

なぜですか?どのように解決することができますか?

JS:

$(document).ready(function(){ 

    var api = 'http://api.openweathermap.org/data/2.5/weather?id=2172797&APPID=35000cdad97645316c048563e4183021'; 

    $.getJSON(api, {format:'json'},function(data){console.log(data.coord.lon)}); 


}); 

CodePen:事前にhttps://codepen.io/elivanrock/pen/zKoYEj?editors=1011

ありがとう!

+1

**ブラウザのを確認してください** コンソール。 HTTP-HTTPsの問題。 HTTP経由でcodepenをロードすると、コードが機能します。 – yuriy636

+0

おかげでたくさんのユーリ! –

答えて

1

あなたは自分のコールバック関数がこの方法を追加し、openweathermap.comからJSONPを使用してデータを取得することができます。以下

http://api.openweathermap.org/data/2.5/weather?id=2172797&APPID=35000cdad97645316c048563e4183021&callback=myfunc 

例:

$.ajax({ 
 
    url: "http://api.openweathermap.org/data/2.5/weather", 
 
    jsonp: "callback", 
 
    dataType: "jsonp", 
 
    data: { 
 
     id: "2172797", 
 
     APPID: "35000cdad97645316c048563e4183021" 
 
    }, 
 
    success: function(response) { 
 
     console.log(response); // server response 
 
     $('.current').html('<img src="http://openweathermap.org/img/w/' + response.weather[0].icon + '.png" /> ' + response.weather[0].main); 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="current"></div>

関連する問題