Застрял на WeatherApp для свободного вызова кода
На этот раз я застрял на JS для WeatherApp. Я использую один из учебных пособий о том, как делать шаг за шагом (с моей изюминкой), но основа кода без моего уклона. Но
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);
я не могу записать переменную погоды в консоль, не знаю почему
Я сидел по этому вопросу около 4 часов и еще несколько раньше, и я не могу найти никакого решения для этого. Может ли кто-нибудь быть таким добрым и указать мне правильное направление?
1 ответ
Решение
Убедитесь, что вы правильно включили библиотеку 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>