2017-11-18 50 views
-1

我在FCC上構建了一個天氣應用程序,我因爲我的代碼無法正常工作而卡住了。我沒有收到瀏覽器訪問我的位置的彈出式請求。 我在我的button.click函數中封裝了該彈出式請求。 我是新手,我感到非常沮喪。我需要幫助!!

這裏是我的代碼:

 var Api= "https://fcc-weather-api.glitch.me/api/current?"; 

     $(document).ready(function(){ 

     $("button").click(function(){ 

     if (navigator.geolocation) { 

     navigator.geolocation.getCurrentPosition(function(position) { 
         
     var lat = "lat=" + position.coords.latitude ; 
         
     var lon = "lon=" + position.coords.longitude; 

    $.get(Api+lat+"&"+ lon, function(result){ 

     $("#city").text(result.main.name); 

     $("#country").text(result.sys.country); 

     $("#weather").text(result.weather[0].main); 

     $("#w-icon").text(result.weather[0].icon); 

     $("#temp").text(result.main.temp); 

     $("humidity").text(result.main.humidity); 

     }); 


     }); 


     }); 
      else { 

     console.log ("Geolocation is not supported by this browser"); 
    } 
     }); 

https://codepen.io/Ojomiba/pen/dVgQeQ

+0

爲什麼投票。當我嘗試使用該網站進行學習時,我將被禁止詢問更多問題。 – user8953638

回答

1

打開您的瀏覽器的開發者工具。

看看控制檯。

閱讀錯誤消息


Uncaught ReferenceError: lat is not defined

您已經定義lat傳遞給getCurrentPosition裏面的功能,但是你要使用它該功能。

您需要移動試圖使用它的代碼,以便它在函數內。