簡體   English   中英

在JavaScript中使用$ .get遇到麻煩

[英]Having trouble with $.get in JavaScript

我是JavaScript的新手。 現在,我已經解決了這個問題,我遇到了一個非常奇怪的錯誤。 我正在構建一個應用程序以顯示您所在位置的當前天氣,為此,我需要對天氣數據進行API調用。 我從這里獲取天氣數據,首先,我的代碼正在運行並獲取當地天氣(對於給定的經度和緯度)。 但是,我在編輯代碼以添加功能時,我的代碼突然停止工作。 我試圖修復它,但是我被弄糊塗了,我不知道我弄壞了什么。 我希望你們能提供幫助!

這是我的代碼供參考:

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

我將console.log(data)放在API調用下面,以查看發生了什么,但沒有輸出任何內容。 很困惑,請幫忙!

我在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 + "\° 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" /> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM