【问题标题】:$.getJSON adds ' http://localhost:3000' to api URL$.getJSON 将“http://localhost:3000”添加到 api URL
【发布时间】:2016-08-26 23:20:38
【问题描述】:

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

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

});

【问题讨论】:

    标签: jquery weather-api


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢,遗憾的是文档不够清晰
    • 任何关于 URL 的体面教程都应该解释它们是如何构建的。
    猜你喜欢
    • 2019-07-07
    • 2016-07-21
    • 2021-03-11
    • 2018-11-22
    • 1970-01-01
    • 1970-01-01
    • 2021-09-28
    • 2018-10-28
    • 2022-09-28
    相关资源
    最近更新 更多