Javascript 如何从AJAX请求返回值?

Javascript 如何从AJAX请求返回值?,javascript,ajax,Javascript,Ajax,我有一个函数,它用var关键字声明一个变量。然后,它启动一个AJAX请求来设置变量的值,然后从函数返回该变量 然而,我的实现失败了,我不知道为什么 下面是代码的简化版本 function sendRequest(someargums) { /* some code */ var the_variable; /* some code */ request.onreadystatechange = //here's that other

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

这与作用域无关,而是与异步处理有关。

函数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);

AJAX请求是异步的。您的sendRuest函数正在执行,AJAX请求正在发出,但它是异步发生的;因此,在执行AJAX请求(以及您的onreadystatechange处理程序)之前,将执行sendRuest的其余部分,因此返回时,\u变量
是未定义的

实际上,您的代码的工作方式如下:

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

     var the_variable;

     /* some code */

     return the_variable;
}

var data = sendRequest(someargums);
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
        }
}
过了一段时间,您的AJAX请求完成了;但是已经太晚了

您需要使用一种称为回调的方法:

你以前可能有过

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

  // do something;
}
你应该做:

function () {
  sendRuest(args, function (theResult) {
     // do something
  });
};
并修改
sendRuest
,如下所示:

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

     var the_variable;

     /* some code */

     return the_variable;
}

var data = sendRequest(someargums);
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
        }
}

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

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请求未完成。您需要使用计时器来检查_variable.data(非常糟糕)的值,或者使用其他答案中所述的回调


塞吉奥。

怎么会失败?
request
在哪里声明和设置?已贴花并设置良好。这里没什么好担心的。这看起来像是范围问题吗?类似于:这可能是一个解决方案。谢谢,描述得很好。我非常感谢你!