2017-01-31 7 views
0

簡単な天気予報ページを作成しようとしています。私は、ユーザーの場所の座標を取得し、その後Weather Underground APIからロケーションの天気情報を取得したいと考えています。私は自分の座標のJSONレスポンスを見ることができます。しかし、私はAJAX関数を使用してJSONを解析し、HTMLで表示する際に問題が発生しています。以下は私のコードWeather Underground APIからJSONレスポンスを解析できません

HTMLです:

<div class="container-fluid text-center"> 
<body> 
<div id="forecast"> 
<h1>Weather at <span id="location"> </span></h1> 
<div id="imgdiv"> 
<img id="img" src=""/> 
</div> 
<p>It is currently <span id="temp"> </span>°C with <span id="desc"> </span></p> 
<p>Wind: <span id="wind"></span></p> 

</body> 
</div> 

jQueryの。

var Geo={}; 
    var temp_f 
     //var key = ‘4d6eb96f1aa092b2’; 

     if (navigator.geolocation) { 
     navigator.geolocation.getCurrentPosition(success,error); 
     } 
     else { 
     alert('Geolocation is not supported'); 
     } 

     function error() { 
     alert("That's weird! We couldn't find you!"); 
     } 

    function success(position) { 
       Geo.lat = position.coords.latitude; 
       Geo.lng = position.coords.longitude; 
       Geo.url = "http://api.wunderground.com/api/4d6eb96f1aa092b2/forecast/geolookup/conditions/q/" + Geo.lat + "," + Geo.lng + ".json"; 

    $.ajax({ 
    url : Geo.url, 
    dataType : "json", 
    success : function(url) { 
     location = url['location']['city']; 
     temp_f = url['current_observation']['temp_f']; 
     $("#temp").html(temp_f); 
    } 
    }); 

} 

場所とtemp_fの値はundefinedです。私は非常によく似た投稿Weather Underground API with Ajaxを参照しましたが、何が問題なのか把握できませんでしたか?私はこれを新しいですので、何か基本的なものが欠けているならば容赦してください。

+0

行う警告(JSON.stringify(URL))、またははconsole.log(JSON.stringify(URL))。また、応答が何を得るを参照してくださいコードを試してみてくださいここに投稿してください – Jigarb1992

+0

それは '未定義'を与える​​ – Jio

+0

Geo.latとGeo.lngの値は何ですか?例えば – Jigarb1992

答えて

1

私はチェックのためにそれが正常に動作します:私はあなたのコードで見つかったhttps://jsfiddle.net/jigarb1992/q1w8usq9/

問題ここlocation = url['location']['city'];かもしれあり、javascriptlocationは、所定の場所にリダイレクトされます。

私は変更を行う必要がありjsfiddle

+0

はい、今動作するようです。しかし問題があるかもしれませんが、変数は不要です。ご協力いただきありがとうございます – Jio

関連する問題