2016-08-26 49 views
0

error
我建立天氣應用程序內,當我打電話API,$.getJSON增加了我的本地ADDRES。

GET http://localhost:3000/api.openweathermap.org/data/2.5/weather?lat=50.064650099999994&lon=19.9449799&APPID=f7dcb8e5d6a1f2126a2080a1e0d17b5a 404(未找到)

$("#getLocation").on("click", function() { 
    if (navigator.geolocation) { 
     navigator.geolocation.getCurrentPosition(function(position) { 
      $("#location").html("latitude: " + position.coords.latitude + "<br>longitude: " + position.coords.longitude); 
      lat = position.coords.latitude; 
      lon = position.coords.longitude; 

      api = "api.openweathermap.org/data/2.5/weather?lat=" + lat + "&lon=" + lon + "&APPID=f7dcb8e5d6a1f2126a2080a1e0d17b5a"; 

      console.log(api); 

      $.getJSON(api, function(json) { 
       $("#api").html(json); 
      }); 
     }); 
    } 

}); 

回答

0

由於URL不包含//servername,它解釋爲從當前頁面的路徑相對URL。使用:

 api = "//api.openweathermap.org/data/2.5/weather?lat=" + lat + "&lon=" + lon + "&APPID=f7dcb8e5d6a1f2126a2080a1e0d17b5a"; 
+0

感謝,不幸的是文檔不夠清楚 – KAT

+0

任何一個體面的URL教程應該解釋它們是如何構建的。 – Barmar

相關問題