[英]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.