Accenzer 2017-01-02 03:30 采纳率: 66.7%
浏览 3475
已采纳

关于js匿名函数的返回值的获取问题

我还在读js的基础书籍,所以对一些js原理了解不是全面,所以问题如有愚昧,还请见谅。

 function asynSubmit(sData,action,method="POST"){
    var httpRequest = new XMLHttpRequest();
    var rMessage="请求未发送";
    httpRequest.open(method,action);
    httpRequest.setRequestHeader("content-type","application/x-www-form-urlencoded");
    httpRequest.send(sData);
    httpRequest.onreadystatechange=function(){
        if(httpRequest.readyState===4){
            console.log("4");
            if(httpRequest.status===200){
                console.log("200");
                var rData=httpRequest.responseText;
                //JSON.parse(httpRequest.responseText);
                return rData;
            }else{
                return "服务器异常";
            }
        }else{
            return "服务器未响应";
        }
    }
}

如上述代码,我想封装一个ajax的函数。想让函数返回responseText的值,因为onredeaychang调用了匿名函数,在匿名函数里返回的值我要怎么在外层函数获取?或者有其他什么方法能达到我的目的?我也试过在外层函数定义变量,在匿名函数里为变量赋值,但由于匿名函数绑定了事件,所以这样做直接返回空值。

自己想了很久,也百度过了,实在想不出解决办法,还请各位不吝赐教。

  • 写回答

2条回答

  • Go 旅城通票 2017-01-02 05:13
    关注

    你要reutrn也要在asynSubmit最后,在匿名函数里面return是匿名函数的返回值,并不是asynSubmit的。而且你设计到ajax,只有改为同步asynSubmit return才能获取到值,异步的只能通过回掉形式来获取值

    同步

      function asynSubmit(sData,action,method="POST"){
     var rst=false;/////////////
    
        var httpRequest = new XMLHttpRequest();
        var rMessage="请求未发送";
        httpRequest.open(method,action,false);//////////////改同步
        httpRequest.setRequestHeader("content-type","application/x-www-form-urlencoded");
        httpRequest.send(sData);
        httpRequest.onreadystatechange=function(){
            if(httpRequest.readyState===4){
                console.log("4");
                if(httpRequest.status===200){
                    console.log("200");
                    var rData=httpRequest.responseText;
                    //JSON.parse(httpRequest.responseText);
                    rst= rData;////////////////////////////////////
                }else{
                    rst= "服务器异常";////////////////////////////////////
                }
            }else{
                rst= "服务器未响应";////////////////////////////////////
            }
        }
            return rst////////////////////////////////////
    }
    var data=asynSubmit('xxx','xxxx','xxxxx')//
    alert(data)
    

    异步,只能回掉,无法return

    
     function asynSubmit(sData,action,method="POST",callback){
        var httpRequest = new XMLHttpRequest();
        var rMessage="请求未发送";
        httpRequest.open(method,action);
        httpRequest.setRequestHeader("content-type","application/x-www-form-urlencoded");
        httpRequest.send(sData);
        httpRequest.onreadystatechange=function(){
            if(httpRequest.readyState===4){
                console.log("4");
                if(httpRequest.status===200){
                    console.log("200");
                    var rData=httpRequest.responseText;
                    //JSON.parse(httpRequest.responseText);
                   callback( rData);////////////////////
                }else{
                    callback( "
    

    服务器异常");////////////////////
    }
    }else{
    callback( "服务器未响应");////////////////////
    }
    }
    }

    asynSubmit('xxx','xxxx','xxxxx',function(data){alert(data)})//

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛