繁体   English   中英

带有getJSON的第二个函数未执行

[英]Second function with getJSON not executing

就像标题所说的那样,我正在尝试制作一个天气应用程序,该应用程序可以获取用户的位置并获取他们的当地天气。 我被困在不会执行的第二个功能和console.log();上。 没有提供任何输出。 任何帮助将不胜感激。

$(document).ready(function () {
    function getLocation() {
        $.getJSON("http://ipinfo.io", function (response) {
            var cc = response.country;
            var city = response.city;
            var state = response.region;
            $(".city").html(city + "," + state);
            console.log(cc);
            var url = "api.openweathermap.org/data/2.5/weather?q=" + city + "," + cc + "&APPID=1d0e324c03cd19ecf0abf20ac2708666";
            console.log(city);
            console.log(url);
            getWeather(url);

        });
    }
    function getWeather(url) {
        $.getJSON(url, function (response) {

            console.log(url);
            $(".temp").html(Math.round(response.main.temp));

        });
    }
    getLocation();

});

以这种方式更改网址:

var url = "http://api.openweathermap.org/data/2.5/weather?q="+city+","+cc+"&APPID=1d0e324c03cd19ecf0abf20ac2708666";

你错过了http

Weather API的URL不正确,您需要对API使用http:// ,否则将其视为相对URL。

采用

var url = "http://api.openweathermap.org/data/2.5/weather?q=" + ...

 function getLocation() { $.getJSON("http://ipinfo.io", function(response) { // console.log(response); var cc = response.country; var city = response.city; var state = response.region; //Updated the API var url = "http://api.openweathermap.org/data/2.5/weather?q=" + city + "," + cc + "&APPID=1d0e324c03cd19ecf0abf20ac2708666"; getWeather(url); }); } function getWeather(url) { $.getJSON(url, function(response) { console.log(response); }); } getLocation(); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM