与美元的麻烦。在JavaScript

时间:2023-01-24 22:25:54

I'm a newbie to JavaScript. Now that I got that out of the way, I'm experiencing a very weird error. I'm building an application to show the current weather at your location, and to do this I needed to make an API call for the weather data. I got the weather data from here and at first, my code was working and getting the local weather (for a given latitude and longitude). However, I was making edits to my code to add functionality and my code suddenly stopped working. I tried to fix it but I'm flummoxed, I have no idea what I broke. I'm hoping you guys can help!!

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

Here is my code for reference:

以下是我的代码以供参考:

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

I put a console.log(data) below the API call to see what is happening, but it doesn't output anything. Very confused, please help!

我在API调用下面放置了一个console.log(data)来查看发生了什么,但是它没有输出任何内容。很困惑,请帮助!

1 个解决方案

#1


2  

I run this code in firefox and get json data from api.openweathermap.org

我在firefox中运行这段代码,并从api.openweathermap.org获取json数据

Check if your browser support navigator.geolocation and permit share your location.

检查浏览器是否支持导航器。地理位置和允许共享你的位置。


the following snippet overrides navigator.geolocation.getCurrentPosition and invokes the inner fn with .bind to force it fire.

下面的代码片段覆盖了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" />

#1


2  

I run this code in firefox and get json data from api.openweathermap.org

我在firefox中运行这段代码,并从api.openweathermap.org获取json数据

Check if your browser support navigator.geolocation and permit share your location.

检查浏览器是否支持导航器。地理位置和允许共享你的位置。


the following snippet overrides navigator.geolocation.getCurrentPosition and invokes the inner fn with .bind to force it fire.

下面的代码片段覆盖了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" />