js文件代码如下
function searchPointByName(area) {
var weatherData= new Object();
$.ajax({
url:'http://wthrcdn.etouch.cn/weather_mini?city='+area,
type:'POST',
dataType:'jsonp',
success:function (data) {
if(data.status=='1002'){
alert('没有找到该城市,请重新选择');
return false
}else{
weatherData.ganmao=data.data.ganmao;
weatherData.wendu=data.data.wendu;
weatherData.yesterday=data.data.yesterday;
weatherData.city=data.data.city;
}
}
});
console.log(weatherData);
console.log(weatherData.city);
return weatherData;
}
为什么在主HTML页面里var weatherData=searchPointByName(area);console.log(weatherData)在控制台打印出来的是
注意Object {}里面是有内容的,点击打开后是这样
为什么通过weatherData.XXX 如 weatherData.city 拿到里面的值为undefind
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
想说一句, 为什么 当
status = 1002知道在里面 return false了,成功了 就在外面return 了ajax是异步的
weatherData 是一个对象哦 所以那看到就是这样喽 你的js面向对象不知道学的怎么样?