我正在嘗試創建一個簡單的天氣頁面。我想檢索用戶位置的座標,並隨後從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,但還是無法弄清楚是什麼問題?我是新的,所以請原諒,如果我錯過了一些基本的東西。
做警報(JSON.stringify(URL))或執行console.log(JSON.stringify(URL)),看看有什麼迴應你,也請代碼張貼在這裏 – Jigarb1992
它給'未定義' – Jio
Geo.lat和Geo.lng的價值是什麼?例如: – Jigarb1992