手写的一个ajax类,为何在ie7.0运用时正常,在ie6.0发生错误,提示"对象不支持此属性或方法" 请教一下高手

解决方案 »

  1.   

    用的对象太高级了,ie6不支持呗。
    比如说你ie7里面有个功能是新添加的,ie6里面没有的,那你用的话,肯定就是对象不支持了..
      

  2.   

    IE 需要用 ActiveX 创建 xmlhttp
      

  3.   

    ajax classfunction class_ajax()
    {
    this.ID = "ajax";
    /**
     * xml请求对象
     */
    this.xmlHttp = null;
    /*
     * 请求的目标url
     */
    this.url = "";
    /**
     * http请求的方法 GET or POST
     */
    this.method = "";
    /**
     * 状态改变的事件触发函数
     */
    this.event = "";
    /**
     * 是否为异步连接
     */
    this.async = true;
    /**
     * REQUEST参数
     */
    this.params = new Array;


    /**
     * 初始化xmlHttpRequest对象
     *
     */
    this.init = function()
    {
    if(this.xmlHttp == null)
    {
    if (window.XMLHttpRequest)
        {
            this.xmlHttp = new XMLHttpRequest();
        }
         else if (window.ActiveXObject)
         {
            this.xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            if(!this.xmlHttp)
            {
             this.xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
            }
            
    //         var msxmls = new Array('Msxml2.XMLHTTP.7.0','Msxml2.XMLHTTP.6.0','Msxml2.XMLHTTP.5.0','Msxml2.XMLHTTP.4.0','Msxml2.XMLHTTP.3.0','Msxml2.XMLHTTP','Microsoft.XMLHTTP');
    //             for (var i = 0; i < msxmls.length; i++)
    //             {
    //                 try 
    //                 {
    //                         this.xmlHttp = new ActiveXObject(msxmls[i]);
    //                 }catch (e){}//         }
        }
    }
    if(this.xmlHttp == null)
    {
    alert("invalid xmlHttpRequest!");
    return false;
    }
    }//end function

    /**
     * 设置目标请求方法
     */
    this.set_method = function(method)
    {
    if(method != 'POST' && method != 'GET')

    alert("invalid method!");
    return false;
    }
    this.method = method;
    }//end function

    /**
     * 获取目标请求方法
     */
    this.get_method = function()
    {
    return this.method;
    }//end function

    /**
     * 设置状态该表要触发的事件
     */
    this.set_event = function(event)
    {
    this.event = event;
    }//end function

    /**
     * 获取 event
     */
    this.get_event = function()
    {
    return this.event;
    }//end function

    /**
     * 设置要请求的目标url
     */
    this.set_url = function(url)
    {
    this.url = url;
    }//end function

    /**
     * 获取 url
     */
    this.get_url = function()
    {
    return this.url;
    }//end function

    /**
     * 添加REQUEST 参数
     */
    this.add_param = function(key, val)
    {
    var tmpCount = this.params.length;
    this.params[tmpCount] = key + "=" + val; 
      }//end function
     
      /**
       * 设置请求为异步连接
       */
      this.set_async = function()
      {
      this.async = true;
      }//end function
     
      /**
       * 取消异步连接,即设置为同步连接
       */
      this.set_sync = function()
      {
      this.async = false;
      }//end function
     
      /**
     * 发送请求
     */
    this.submit = function()
    {
    this.xmlHttp.onreadystatechange = this.event;
          var send_params = null;
    if(this.params.length > 0)
    {
    send_params = this.params.join('&');
    if(this.method == 'POST')
    {
    this.xmlHttp.open(this.method, this.url, this.async);
    //this.xmlHttp.setRequestHeader("Method", this.method + this.url + " HTTP/1.1");
    this.xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    this.xmlHttp.send(send_params);
    }else
    {
    this.xmlHttp.open(this.method, this.url + "?" + send_params, this.async);
    this.xmlHttp.send(null);
    }
    }else
    {
      this.xmlHttp.open(this.method, this.url, this.async);
      this.xmlHttp.send(send_params);
    }

    }//end function

    /**
     * return Data
     * @param string type : "xml" or "text"
     */
    this.get_content = function(type)
    {
    if(type == 'xml')
    {
    return this.xmlHttp.responseXML.documentElement;
    }
    else
    {
    return this.xmlHttp.responseText;
    }
    }

    /**
     * 检测事件是否完成
     */
    this.complete = function()
    {
    if(this.xmlHttp.readyState == 4)
    {
    return true;
    }
    return false;
    }//end function

    /**
     * 检测是否处在数据读取状态
     */
    this.isSending = function()
    {
    if(this.xmlHttp.readyState == 1)
    {
    return true;
    }
    return false;
    }//end function

    /*
     * 检测请求是否成功
     */
    this.success = function()
    {
    if (this.complete() && this.xmlHttp.status == 200) {
      return true;       
            }
            return false;
    }//end function

    /**
     * 获取消息提示
     */
    this.get_statusText = function()
    {
    return this.xmlHttp.statusText;
    }//end function
    }//end class
      

  4.   

    var JPubMenu = {
    ajax : [],
    elm : [],
    loading : '<div style="margin:0 auto;text-align:center;"><img src="Web_Images/Loading/loading.gif" /><br /> 数据加载中,请稍候 . . . . . .</div>',

    get_second_menu : function(FuncBaseID,elm){
    var elm = document.getElementById(elm); var ajax = new class_ajax();
    ajax.init();
    ajax.set_method('GET');
    ajax.set_event(get_zone_call);
    ajax.set_url("web/include/GetMenuData.aspx");
    ajax.add_param("FuncBaseID", FuncBaseID);
    ajax.submit();

    function get_zone_call(){
    if (ajax.isSending())
    {
    elm.innerHTML = JPubMenu.loading;
    }
    if(ajax.complete())
    {

    if(ajax.success())
    {
    var response = ajax.get_content('txt');
    elm.innerHTML = response;
    }
    else
    {
    elm.innerHTML = "";
    }
    }
    }
    }
    };
      

  5.   

    运用这样onclick="JPubMenu.get_second_menu('75', 'menuList');"
    为何在ie7.0运用时正常,在ie6.0发生错误,提示"对象不支持此属性或方法"  
      

  6.   

    看过你的代码,发现你没有弄清楚IE6支持xmlhttp的版本顺序,/*初始化XMLHttpRequest对象*/try { request=new XMLHttpRequest; }
    catch(e) {
    try { request=new ActiveXObject("MSXML2.XMLHTTP"); }
    catch(e2) {
    try { request=new ActiveXObject("Microsoft.XMLHTTP");}
    catch(e3) { request = false; }
    }
    }这样写着就OK了