简体   繁体   中英

Second function with getJSON not executing

As the title says, I am trying to make a weather app which takes the users location and gets their local weather. I am stuck on the second function which will not execute and the console.log(); isn't providing any output. Any help would be greatly appreciated.

$(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();

});

change the url in this way:

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

you were missing http

URL for Weather API is incorrect, You need to use http:// with API otherwise its treated as relative URL.

Use

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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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