我试图从here得到天气预报。这工作正常,我得到了我的价值。如果我做一个正常的Ajax调用它工作正常。ajax调用返回值,但变量为空
但是:
function Weather() {
var self = this,
weatherUrl = 'http://api.openweathermap.org/data/2.5/weather?id=',
value = null;
function getWeatherForCity(id) {
$.ajax({
url: weatherUrl + id,
async: false,
success: function (data) {
console.log(data);
value = data;
}
});
}
self.getWeatherForCity = function (id) {
var cast = value;
console.log(cast);
return cast;
};
}
召唤:
weather = new Weather();
console.log(weather.getWeatherForCity('2878234'));
如果我通过这些功能的调试,我得到了很好的结果,成功的回调函数里面,但投变量为空,喜欢它从未被感动过?
有人可以向我解释吗?
让我们开始吧wi “async:false”这个事实是非常糟糕的,并且是不赞成的事情。 – Regent 2014-10-29 07:55:26
好吧,我知道,但这不是真的答案。 – Ipad 2014-10-29 07:56:21
问题是,你从来没有真正调用'getWeatherForCity()'。因此,结果。 – dfsq 2014-10-29 07:59:37