程宇寒 2018-03-15 06:11 采纳率: 100%
浏览 1942
已结题

javascript中使用ajax时设置回调函数,我想向回调函数中传参?该怎么传参?

javascript中使用ajax时设置回调函数,我想向回调函数中传参?该怎么传参?
getCitysByParentId是回调函数,我现在想往getCitysByParentId这个回调函
数中传liId这个参数进去,该怎么处理?

 <script type="text/javascript">
 var xmlHttpRequest;
    function createXmlHttpRequest() {
        //判断不同浏览器,采用不同方式创建XMLHttpRequest对象
        if (window.ActiveXObject) {
            //IE浏览器
            try {
                xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {
                xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
            }
            return xmlHttpRequest;
        } else if (window.XMLHttpRequest) {
            //其他浏览器如Firefox、Chrome等等
            return new XMLHttpRequest();
        }
    }


    //我是回调函数
    function getCitysByParentId(currentLiId) {
    //做一些事情,代码省略...
    //回调函数中要使用currentLiId这个参数,所以需要外界传一个参数进来
    }

    function getCitys(cityId, liId) {


        //1.创建XMLHttpRequest对象
        xmlHttpRequest = createXmlHttpRequest();

        //getCitysByParentId是回调函数,我现在想往getCitysByParentId这个回调函
        数中传liId这个参数进去,该怎么处理?

        //2.设置回调函数
        xmlHttpRequest.onreadystatechange = getCitysByParentId;

        /*
        getCitysByParentId是回调函数,我现在想往getCitysByParentId这个回调函
        数中传liId这个参数进去,该怎么处理?
        */


        //请求的目标地址
        var url = "${pageContext.request.contextPath}/JsonCity?provinceId="
                + cityId;
        //3.初始化XMLHttpRequest组件
        //addTimestampParameter(url)函数不太好的就是,URL本身可能带有一些参数,那就要判断是加"?"还是加"&"了
        //xmlHttpRequest.open("GET", addTimestampParameter(url), true);
        xmlHttpRequest.open("GET", url, true);
        //xmlHttpRequest.open("POST", url, true);
        //推荐使用下面这种方式,简洁灵活,不影响URL参数
        xmlHttpRequest.setRequestHeader('If-Modified-Since', '0');
        //4.发送请求
        xmlHttpRequest.send(null);
        //显示加载图片
        showLoading(document.getElementById(liId),
                "${pageContext.request.contextPath}/img/loading1.gif");
    }

 </script>
  • 写回答

3条回答 默认 最新

  • qq_31817893 2018-03-15 06:19
    关注

    var xmlHttpRequest;
    function createXmlHttpRequest() {
    //判断不同浏览器,采用不同方式创建XMLHttpRequest对象
    if (window.ActiveXObject) {
    //IE浏览器
    try {
    xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e) {
    xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
    }
    return xmlHttpRequest;
    } else if (window.XMLHttpRequest) {
    //其他浏览器如Firefox、Chrome等等
    return new XMLHttpRequest();
    }
    }

    //我是回调函数
    function getCitysByParentId(currentLiId) {
    //做一些事情,代码省略...
    //回调函数中要使用currentLiId这个参数,所以需要外界传一个参数进来
    }
    
    function getCitys(cityId, liId) {
    
    
        //1.创建XMLHttpRequest对象
        xmlHttpRequest = createXmlHttpRequest();
    
        //getCitysByParentId是回调函数,我现在想往getCitysByParentId这个回调函
        数中传liId这个参数进去,该怎么处理?
    
        //2.设置回调函数
        xmlHttpRequest.onreadystatechange = getCitysByParentId(liId);  //直接这么穿就行吧
    
        /*
        getCitysByParentId是回调函数,我现在想往getCitysByParentId这个回调函
        数中传liId这个参数进去,该怎么处理?
        */
    
    
        //请求的目标地址
        var url = "${pageContext.request.contextPath}/JsonCity?provinceId="
                + cityId;
        //3.初始化XMLHttpRequest组件
        //addTimestampParameter(url)函数不太好的就是,URL本身可能带有一些参数,那就要判断是加"?"还是加"&"了
        //xmlHttpRequest.open("GET", addTimestampParameter(url), true);
        xmlHttpRequest.open("GET", url, true);
        //xmlHttpRequest.open("POST", url, true);
        //推荐使用下面这种方式,简洁灵活,不影响URL参数
        xmlHttpRequest.setRequestHeader('If-Modified-Since', '0');
        //4.发送请求
        xmlHttpRequest.send(null);
        //显示加载图片
        showLoading(document.getElementById(liId),
                "${pageContext.request.contextPath}/img/loading1.gif");
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝