我有一个函数用var
关键字声明一个变量。然后它启动一个AJAX请求来设置变量的值,然后从该函数返回该变量。如何从AJAX请求中返回值?
但是,我的执行失败,我不知道为什么。
下面是代码的简化版本;
function sendRequest(someargums) {
/* some code */
var the_variable;
/* some code */
request.onreadystatechange =
//here's that other function
function() {
if (request.readyState == 4) {
switch (request.status) {
case 200:
//here the variable should be changed
the_variable = request.responseXML;
/* a lot of code */
//somewhere here the function closes
}
return the_variable;
}
var data = sendRequest(someargums); //and trying to read the data I get the undefined value
失败如何?请求声明和设置在哪里? – 2010-06-02 09:08:32
这是decalred和设置好。这里没什么可担心的。它看起来不像范围问题吗? – 2010-06-02 09:10:11
与此类似:http://stackoverflow.com/questions/2631250/jquery-wait-for-ajax-call-to-be-over-before-continuing – 2010-06-03 15:58:09