$ .getJSON将'http:// localhost:3000'添加到api URL

时间:2016-08-26 23:20:38

标签: jquery weather-api

error
我正在构建Weather-App,当我调用API时,$.getJSON会添加我的localhost地址。

  

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);
            });
        });
    }

});

1 个答案:

答案 0 :(得分:0)

由于URL不包含//servername,因此它被解释为当前页面路径中的相对URL。使用:

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