代码之家  ›  专栏  ›  技术社区  ›  kilrizzy

XML ActiveXObject IE.Object不支持此操作

  •  1
  • kilrizzy  · 技术社区  · 16 年前

    Object doesn't support this action
    

    我正在使用以下功能:

       function createRequestObject(){
         var request;
        try {
          request = new XMLHttpRequest();
        } catch (trymicrosoft) {
            try {
                    request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (othermicrosoft) {
                    try {
                            request = new ActiveXObject("Microsoft.XMLHTTP");
                    } catch (failed) {
                            request = false;
                    }
            }
        }
        return request;
    }
    

    然后用以下方式称呼它:

      var xhttp = createRequestObject();
        xhttp.open("GET","myfile.xml",false);
    ...
    

    有什么想法吗??

    2 回复  |  直到 16 年前
        1
  •  2
  •   meder omuraliev    16 年前

    尝试为请求指定一个局部变量, var request

    /** XHConn - Simple XMLHTTP Interface - bfults@gmail.com - 2005-04-08        **
     ** Code licensed under Creative Commons Attribution-ShareAlike License      **
     ** http://creativecommons.org/licenses/by-sa/2.0/                           **/
    function XHConn()
    {
      var xmlhttp, bComplete = false;
      try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
      catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
      catch (e) { try { xmlhttp = new XMLHttpRequest(); }
      catch (e) { xmlhttp = false; }}}
      if (!xmlhttp) return null;
      this.connect = function(sURL, sMethod, sVars, fnDone)
      {
        if (!xmlhttp) return false;
        bComplete = false;
        sMethod = sMethod.toUpperCase();
    
        try {
          if (sMethod == "GET")
          {
            xmlhttp.open(sMethod, sURL+"?"+sVars, true);
            sVars = "";
          }
          else
          {
            xmlhttp.open(sMethod, sURL, true);
            xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
            xmlhttp.setRequestHeader("Content-Type",
              "application/x-www-form-urlencoded");
          }
          xmlhttp.onreadystatechange = function(){
            if (xmlhttp.readyState == 4 && !bComplete)
            {
              bComplete = true;
              fnDone(xmlhttp);
            }};
          xmlhttp.send(sVars);
        }
        catch(z) { return false; }
        return true;
      };
      return this;
    }
    

    var myConn = new XHConn();
    
    if (!myConn) alert("XMLHTTP not available. Try a newer/better browser.");
    
    var fnWhenDone = function (oXML) { alert(oXML.responseText); };
    
    myConn.connect("mypage.php", "POST", "foo=bar&baz=qux", fnWhenDone);
    
        2
  •  1
  •   Caleb    16 年前

    我想你需要放一个 var

    function createRequestObject(){
        var request;
        try {
            request = new XMLHttpRequest();
        } catch (trymicrosoft) {
            try {
                    request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (othermicrosoft) {
                    try {
                            request = new ActiveXObject("Microsoft.XMLHTTP");
                    } catch (failed) {
                            request = false;
                    }
            }
        }
        return request;
    }
    

    IE经常遇到全局变量的问题。