如何从一个AJAX请求返回一个值? [英] How can I return a value from an AJAX request?

查看:180
本文介绍了如何从一个AJAX请求返回一个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数,声明与 VAR 关键字变量。然后,它将启动一个AJAX请求来设定变量的值,和此变量然后从函数返回。

不过,我的执行失败,我不知道为什么。

下面是的code的简化版本;

 函数sendRequest将(someargums){
     / *一些code * /

     VAR the_variable;

     / *一些code * /

     request.onreadystatechange =
        //这里的其他功能
        功能() {
            如果(request.readyState == 4){
                开关(request.status){
                    案例200:
                        //这里的变量应该改变
                        the_variable = request.responseXML;

        / *很多code * /

        //这里的某处功能关闭
        }

     返回the_variable;
}

VAR数据= sendRequest将(someargums); //并试图读取我得到的不确定值的数据
 

解决方案

AJAX请求是异步的。您sendRuest功能时exectued,正在取得AJAX请求,但它发生异步;所以sendRuest的其余部分被执行,AJAX请求(和你的onreadystatechange的处理)被执行之前,因此 the_variable 是未定义返回时。

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

 函数sendRuest(someargums){
     / *一些code * /

     VAR the_variable;

     / *一些code * /

     返回the_variable;
}

VAR数据= sendRequest将(someargums);
 

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

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

您previously可能有

 函数(){
  VAR theResult = sendRuest(参数);

  // 做一点事;
}
 

您应该做的:

 函数(){
  sendRuest(参数,函数(theResult){
     // 做一点事
  });
};
 

和修改 sendRuest 如下:

 函数sendRuest(someargums,回调){
     / *一些code * /

     //这里的其他功能
     request.onreadystatechange =
        功能() {
            如果(request.readyState == 4){
                开关(request.status){
                    案例200:
                        回调(request.responseXML);

        / *很多code * /

        //这里的某处功能关闭
        }
}
 

I have a function which declares a variable with the var keyword. It then launches an AJAX request to set the value of the variable, and this variable is then returned from the function.

However, my implementation fails and I 'don't know why.

Here's a simplified version of the code;

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

解决方案

AJAX requests are asynchronous. Your sendRuest function is being exectued, the AJAX request is being made but it happens asynchronously; so the remainder of sendRuest is executed, before the AJAX request (and your onreadystatechange handler) is executed, so the_variable is undefined when it is returned.

Effectively, your code works as follows:

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

     var the_variable;

     /* some code */

     return the_variable;
}

var data = sendRequest(someargums);

And then some time later, your AJAX request is completing; but it's already too late

You need to use something called a callback:

Where you previously may have had

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

  // do something;
}

You should do:

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

and modify sendRuest as follows:

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请求返回一个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆