在 JavaScript 中遇到 $.get 问题

Having trouble with $.get in JavaScript

我是 JavaScript 的新手。现在我已经解决了这个问题,我遇到了一个非常奇怪的错误。我正在构建一个应用程序来显示您所在位置的当前天气,为此我需要调用 API 来获取天气数据。我从 here 获得了天气数据,起初,我的代码正在运行并获取当地天气(对于给定的纬度和经度)。但是,我正在编辑我的代码以添加功能,但我的代码突然停止工作。我试图修复它,但我很困惑,我不知道我弄坏了什么。希望大家帮帮忙!!

这是我的代码供参考:

if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
        lat = position.coords.latitude;
        long = position.coords.longitude;
        console.log(lat);
        $.get('http://api.openweathermap.org/data/2.5/weather?lat=' + lat + '&lon=' + long + '&appid=00d71b03a3c50910d8f4f0ea51703bb5', function(data) {
            console.log(data);
            if(lat == 300 || long == 300) {
                alert("Enable your location to see how the weather is");
            } else {
                $("#location").text(data.name);
                var weather = data.weather[0].description;
                $("#weather").text(weather);
                temp = Math.round((data.main.temp - 273.15) * 10) / 10.0;
                $("#temperature").text(temp + "\u00B0 C");
                var icon = data.weather[0].icon;
                $("#picture").attr("src", "http://openweathermap.org/img/w/" + icon + ".png");
            } 
        });
    });
};

我在 API 调用下方放置了一个 console.log(data) 以查看发生了什么,但它没有输出任何内容。很迷茫,求助!

我在 firefox 中 运行 此代码并从 api.openweathermap.org

获取 json 数据

检查您的浏览器是否支持 navigator.geolocation 并允许共享您的位置。


以下代码片段覆盖 navigator.geolocation.getCurrentPosition 并使用 .bind 调用内部 fn 以强制其触发。

$(document).ready(function() {
    var appId = "00d71b03a3c50910d8f4f0ea51703bb5";
    if (navigator.geolocation) {

        // TODO: remove this for production. DEMO/DEBUG usage
        navigator.geolocation.getCurrentPosition = function(fn)  {
            fn.bind(this, { coords : { latitude : 51.507351, longitude : -0.127758 }})();
        }; 

        navigator.geolocation.getCurrentPosition(function(position) {
            try {
          
                lat = position.coords.latitude;
                long = position.coords.longitude;
    
                $.get('//api.openweathermap.org/data/2.5/weather?lat=' + lat + '&lon=' + long + '&appid=' + appId, function(data) {
                    console.log("data := %o", data);        
    
                    if(lat == 300 || long == 300) {
                        alert("Enable your location to see how the weather is");
                    } else {
                        $("#location").text(data.name);
                        var weather = data.weather[0].description;
                        $("#weather").text(weather);
                        temp = Math.round((data.main.temp - 273.15) * 10) / 10.0;
                        $("#temperature").text(temp + "\u00B0 C");
                        var icon = data.weather[0].icon;
                        $("#picture").attr("src", "//openweathermap.org/img/w/" + icon + ".png");
                    } 
                });
            } catch(error) {
                console.log("err := %o", error);
            }            
        });
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<label for="location">Location</label><span id="location"></span>
<label for="weather">Weather</label><div id="weather"></div>
<label for="temperature">Temperature (F)</label><span id="temperature"></span>
<img id="picture" src="" width="200px" height="200px" />