所以我试图从开放天气api示例获取json请求。但是由于某种原因,当我去调用$ .each方法中的键值时,它说键的值是不确定的。你们可以看看代码,看看它缺少什么吗?
function getGs(data) {
$.each(data.main,function(i,wather){
console.log(weather.humidity);
})
}
$(document).ready(function() {
var bggAPI = "http://api.openweathermap.org/data/2.5/weather?lat=35&lon=139?";
$.ajax({
dataType: "json",
url: bggAPI
});
getGs();
});
最佳答案
$.ajax
发出异步请求。
成功完成后,将触发success
,并且您错过了它。data.main
不是数组,而是对象。data.weather
是一个数组。
function getGs(data) {
console.log(data.main.humidity);
$.each(data.weather,function(i, weather) {
console.log(weather.main);
})
}
$(document).ready(function() {
var bggAPI = "http://api.openweathermap.org/data/2.5/weather?lat=35&lon=139?";
$.ajax({
dataType: "json",
url: bggAPI,
success:getGs
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
关于javascript - JSON请求对象未定义,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31866700/