【问题标题】:How can I return a value from an AJAX request? [duplicate]如何从 AJAX 请求中返回值? [复制]
【发布时间】:2010-06-02 09:05:27
【问题描述】:

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

【问题讨论】:

标签: javascript ajax


【解决方案1】:

AJAX 请求是异步的。正在执行您的 sendRuest 函数,正在发出 AJAX 请求,但它是异步发生的;所以 sendRuest 的其余部分在执行 AJAX 请求(和你的 onreadystatechange 处理程序)之前执行,所以 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
        }
}

【讨论】:

  • 描述得很好。非常感谢!
【解决方案2】:

这与范围无关 - 它与异步处理有关。
函数 sendRuest 在调用 onreadystatechange 函数之前结束。

【讨论】:

    【解决方案3】:

    您不能从创建 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);
    

    【讨论】:

    • 这可能是一个解决方案。谢谢。
    【解决方案4】:

    您可以使用对象来代替纯字符串变量。

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

    塞尔吉奥。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-20
      • 2020-04-17
      • 1970-01-01
      • 2013-03-28
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      • 2017-06-07
      相关资源
      最近更新 更多