2010-06-02 139 views
0

我有一个函数用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 
+0

失败如何?请求声明和设置在哪里? – 2010-06-02 09:08:32

+0

这是decalred和设置好。这里没什么可担心的。它看起来不像范围问题吗? – 2010-06-02 09:10:11

+0

与此类似:http://stackoverflow.com/questions/2631250/jquery-wait-for-ajax-call-to-be-over-before-continuing – 2010-06-03 15:58:09

回答

9

AJAX请求是异步的。你的sendRuest函数正在执行,AJAX请求正在进行,但它是异步发生的;因此在执行AJAX请求(和您的onreadystatechange处理程序)之前,会执行sendRuest的其余部分,因此the_variable在返回时未定义。

实际上,你的代码的工作原理如下:

function sendRuest(someargums) { 
    /* some code */ 

    var the_variable; 

    /* some code */ 

    return the_variable; 
} 

var data = sendRequest(someargums); 

然后一段时间后,你的AJAX请求完成;但它已经太迟了

您需要使用一种叫做回调:

如果你以前可能有过

function() { 
    var theResult = sendRuest(args); 

    // do something; 
} 

你应该这样做:

function() { 
    sendRuest(args, function (theResult) { 
    // do something 
    }); 
}; 

和修改sendRuest为如下:

function sendRuest(someargums, callback) { 
    /* some code */ 

    //here's that other function 
    request.onreadystatechange = 
     function() {     
      if (request.readyState == 4) {  
       switch (request.status) { 
        case 200: 
         callback(request.responseXML); 

     /* a lot of code */ 

     //somewhere here the function closes 
     } 
} 
+0

很好描述。我非常感谢你! – 2010-06-02 09:15:37

2

这不是关于范围 - 它关于异步处理。
函数sendRuest在onreadystatechange函数被调用之前结束。

1

您不能从创建ajax回调的函数返回变量,因为该变量尚未设置。 ajax函数又必须调用另一个回调函数并返回结果。

request.onreadystatechange = 
     function() {     
      if (request.readyState == 4) {  
       switch (request.status) { 
        case 200: 
         //here the variable should be changed 
         the_variable = request.responseXML; 
         the_callback(the_variable); 
+0

这可能是一个解决方案。谢谢。 – 2010-06-02 09:13:16

1

而不是一个普通的字符串变量,您可以使用一个对象。

function sendRuest(someargums) { 

    var the_variable = { 
     data: null, 
     setData: function(data){ this.data = data;} 
    } 

    //here's that other function 
    request.onreadystatechange = 
     function() {     
      if (request.readyState == 4) {  
       switch (request.status) { 
        case 200: 
         //here the variable should be changed 
         the_variable.setData(request.responseXML); 

     } 

    return the_variable; 
} 

无论如何,你的最后一行是行不通的。当函数'sendRuest'结束时,XHR请求未完成。您需要使用计时器来检查'the_variable.data'的值(非常糟糕),或者使用其他答案中所述的回调函数。

Sergio。