zoukankan      html  css  js  c++  java
  • Chrome 下,重复使用 XMLHttpRequest进行Post数据时,遇到一个奇怪的问题

    var http_request; //在外面申明对象,主要为了在updatePage中使用
       
    //无刷新更新内容
    function post(url,parameter)
    {   
        if(http_request==null)
        {
            http_request = getHttpRequest();
        }
           
        if (!http_request)  
        {
            alert("XMLHttpRequest 初始化失败!");
        }
        else
        {
            //在url尾部加一个rnd随机数,这样就不会发生读取ie缓存
            if(url.indexOf("?") > -1)
            {
                url = url + "&rnd=" + Math.random();
            }
            else
            {
                url = url + "?rnd=" + Math.random();
            }
           
           
            http_request.open("POST", url, false); //打开请求 //false 同步, true 异步
            http_request.onreadystatechange = updatePage; //设置回调方法
            http_request.setRequestHeader("Content-length", parameter.length);
            http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            http_request.send(parameter); //发送请求
        }
    }
      
    //回调方法
    function updatePage()
    {   
        if (http_request.readyState == 4)
        {   
            if(http_request.status == 200)          
            {   
                      alert("..."); //
            }
        }
    }

    测试发现,在chrome 26下,如果申明了一个公共的变量:http_request

    然后多次重复使用XMLHttpRequest时,如果Post时,传递了参数(内容),那么会连续执行2次回调函数: updatePage()

    如果Post时没有传参数(内容),只会执行1次回调函数: updatePage()

    在firefox和ie下测试,只会执行1次回调函数: updatePage()

    解决在chrome 26下遇到的这个问题,也很简单,每次使用http_request(XMLHttpRequest)时,都重新初始化一下:

    http_request = getHttpRequest();

  • 相关阅读:
    cs231n线性分类器作业 svm代码 softmax
    Canopy聚类算法
    python numpy
    thingsboard在windows下安装和使用
    用css布局的方法实现如果字符超过一定长度就显示成省略号
    sql2008以上行转列的方法
    sql 2005,2008开启bcp的方法嗯哈步骤
    关于数据库中的科学计数法的处理
    错误:The Controls collection cannot be modified because the control contains code blocks (i.e. ). .
    ORACLE连接字符串里每个参数的具体意思
  • 原文地址:https://www.cnblogs.com/personnel/p/4584885.html
Copyright © 2011-2022 走看看