这次我坚持使用适用于weatherApp的js。我正在逐步使用其中一个教程(我的转折),但代码的主干没有我的扭曲。但是
function getWeather(loc) {
var lat = Math.floor(loc.split(",")[0]);
var lon = Math.floor(loc.split(",")[1]);
var weatherApiUrl = "http://api.openweathermap.org/data/2.5/weather?lat=" + lat + "&lon=" + lon + "&appid=5a1518c003259fdaa613a1aa78664ff9&";
console.log(weatherApiUrl);
$.get(weatherApiUrl, function(weather){
var temperature = weather.main.temp;
var description = weather.weather[0].description.toUpperCase();
var pressure = weather.main.preure;
console.log(weather);
我无法将天气变量记录到控制台,不知道为什么。
答案 0 :(得分:0)
确保正确包含 jQuery库,并查看以下内容:
function getWeather(loc) {
/* I'm not sure this is correct...
var lat = Math.floor(loc.split(",")[0]);
var lon = Math.floor(loc.split(",")[1]);
RATHER USE: */
// API will already float to .toFixed(2), so no need to do Math stuff...
var latLon = loc.split(","),
lat = latLon[0].trim(),
lon = latLon[1].trim();
var weatherApiUrl = "http://api.openweathermap.org/data/2.5/weather?lat=" + lat + "&lon=" + lon + "&appid=5a1518c003259fdaa613a1aa78664ff9";
$.get(weatherApiUrl, function(weather) {
var temperature = weather.main.temp;
var description = weather.weather[0].description.toUpperCase();
// var pressure = weather.main.preure; // preure? WTF?
console.log(weather);
});
}
getWeather("45.815399, 15.966568");

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;