如何从AJAX请求中返回值?

时间:2010-06-02 09:05:27

标签: javascript ajax

我有一个函数,它使用var关键字声明变量。然后它启动一个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

4 个答案:

答案 0 :(得分: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
        }
}

答案 1 :(得分:2)

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

答案 2 :(得分: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);

答案 3 :(得分: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'的值(非常糟糕)或使用其他答案中所述的回调。

塞尔吉奥。