[英]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(); 我被困在不会执行的第二个功能和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 你错过了http
URL for Weather API is incorrect, You need to use http://
with API otherwise its treated as relative URL. Weather API的URL不正确,您需要对API使用http://
,否则将其视为相对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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.