我正在开发一个天气应用程序项目,无法理解为什么第 20 行的 console.log 在下面不起作用:
$(document).ready(function() {
var long;
var lat;
var temp;
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
long = position.coords.longitude;
lat = position.coords.latitude;
var api =
"api.openweathermap.org/data/2.5/weather?lat=" +
lat +
"&lon=" +
long +
"&appid=xxxxxxxxxxxxxxxxxxxxxxx";
//console.log(api);
$("#data").html("latitude: " + lat + "<br>longitude: " + long);
$.getJSON(api, function(json) {
var kelvin = data.main.temp;
console.log(kelvin);
});
});
}
});
body{
height:100vh;
position:relative;
}
.container{
margin:auto;
text-align:center;
padding:20px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
<div class="container">
<h1>Weather App</h1>
<div id="data">location</div>
<div id="temperature">Temperature
<span id="degrees" onclick="">DEGREES</span></div>
<div class="weather">Weather icon</div>
</div>
</body>
我想从 openweathermap api 获取温度,并且我正在测试 kelvin 变量,但它没有显示任何内容。
提前非常感谢,
最佳答案
这不是调用 $.getJSON 的方式。还差得很远。
第一个参数是 URL,您提供的字符串不是 URL,因为 URL 以协议(protocol)开头,例如“https://”。
第二个参数是数据,而不是函数,这是您提供的。
第三个参数是成功回调的位置。
首先进行打印输出,以防取消引用失败。
关于javascript - 为什么 console.log 在这种情况下不起作用?开放天气 map API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45037620/