2016-07-31 170 views
1

不知道問題出在哪裏,但在選定的div中沒有​​顯示任何內容。無法從openWeatherMap api獲取數據JavaScript

$(document).ready(function(){ 
    var lat, lng, data; 

    // get the current location 
    if (navigator.geolocation) { 
     navigator.geolocation.getCurrentPosition(function(position) { 
      lat = position.coords.latitude; 
      lng = position.coords.longitude; 
     }); 
    }else { 
     alert('Geo Location feature is not supported in this browser.'); 
    } 


//get the data from the openWeatherMap API 
    function getData(){ 
    $.getJSON('api.openweathermap.org/data/2.5/weather?lat='+lat+'&lon='+lng+'', function(json){ 
     data = json; 
    });  
    } 
    $('button').on('click', function(){ 
    getData(); 
    $(this).remove(); 
    $('.container').removeClass('hidden'); 
    $('#location').appendTo(data.name); 
    $('#Weather-condition').appendTo(data.weather.main); 
    $('#wind-speed').appendTo(data.wind); 
    }); 

}); 
+0

你檢查任何錯誤控制檯成功的數據顯示? – yuriy636

+0

沒有錯誤@ yuriy636。 – SenDjasni

+0

您是否從您發佈的代碼以外的地方調用'getData()'?請張貼您的HTML。 – yuriy636

回答

1

它看起來像你有一個錯字:getJESON而不是getJSON!

+0

仍然沒有工作。 – SenDjasni

+0

啊,剛發現另一個錯字:lng與大寫g! – csabinho

+0

謝謝你,但它似乎是問題在其他地方@csabinho – SenDjasni

0

,你可以從兩個方面尋找這個問題

  • 你從阿賈克斯得到的數據? PS:你可以添加c.log(數據)
  • 你在你的網頁
+0

之後控制檯 數據變量在這種情況下是UNDEFINED,但爲什麼? – SenDjasni

+0

我認爲你的網址有誤,請檢查'lat'和lnG字段 – Broven