这是一个异步调用,所以你不能像这样返回。
您必须将代码与data
一起移动到回调函数(function(data){}
)中。
function getAreas(){
$.post("/custom_html/weathermap.php",'',
function(data){
//do something with data here, such as calling another function
}, "json");
}
需要一段时间才能让自己的头脑进入异步思维方式,但是你会解决它。基本上,一旦请求被发送,发送请求的代码就完成了。该线程的执行将完成,并且您的浏览器将只是坐在那里,不做任何事情。那么$.post
调用将从weathermap.php
获取数据,并且将调用您的回调函数。这开始了一个全新的执行线程。尝试将它们看作两个完全独立的执行,一个pre-ajax调用和一个post-ajax调用。
下面是一些ASCII善良:
V
|
User clicks button
(or something else happens)
|
|
Your JavaScript runs
|
|
And eventually gets
to the ajax call -------> SERVER ------> Server sends data back
|
|
And your callback is run
on the data and execution
continues from here
|
|
V
不,是行不通的 - 'ret'是不确定的。 – Skilldrick 2010-08-24 11:52:50
不是匿名函数的范围与getAreas的范围相同吗? [edit]显然我错了,对不起:) – 2010-08-24 11:54:11
匿名函数是一个回调函数,只会在'getAreas'返回后才会调用。 – Skilldrick 2010-08-24 11:56:13