如何从 AJAX 请求返回值?

IT技术 javascript ajax
2021-02-25 10:11:48

我有一个函数,它用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个回答

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
        }
}
很好的描述。我非常感谢你!
2021-05-20 10:11:48

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

您不能从创建 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);
那可能是一个解决方案。谢谢。
2021-05-05 10:11:48

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

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

塞尔吉奥。