正如标题所说,我正在尝试制作一个天气应用程序,它可以吸引用户的位置并获取当地的天气。我被困在第二个不会执行的函数和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();
});
答案 0 :(得分:3)
以这种方式更改网址:
var url = "http://api.openweathermap.org/data/2.5/weather?q="+city+","+cc+"&APPID=1d0e324c03cd19ecf0abf20ac2708666";
你错过了http
答案 1 :(得分:2)
Weather API的网址不正确,您需要将http://
与API一起使用,否则将其视为相对网址。
使用
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>
&#13;