javascript - getJSON 未执行的第二个函数

标签 javascript jquery

正如标题所说,我正在尝试制作一个天气应用程序,它可以获取用户的位置并获取他们本地的天气。我陷入了第二个无法执行的函数和 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

关于javascript - getJSON 未执行的第二个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41995327/

相关文章:

javascript - Flot:如何在条形图中设置数字样式?

javascript - 根据推荐链接自动选择 Contact form 7 中的字段

javascript - 如果 J STD TestCase 方法被 RequireJS 包装,如何从命令行运行 JS 测试驱动程序?

javascript - 如何使用javascript给出 float 总计

javascript - 为什么包含在我的 javascript 蛇游戏中不工作

javascript - 为什么我在 JavaScript 中遇到语法错误?

jquery - 我无法使用 jquery 唯一定位定位元素

javascript - ReactJS Props 和冒泡

javascript - 如何将 2 个不同 .htm 文件中的两个表单提交到同一位置

javascript - Web 组件中的触发事件