XML ActiveXObject IE。对象不支持此操作

发布于 2024-08-12 01:36:32 字数 807 浏览 3 评论 0原文

我正在尝试使用 xml 和 javascript。在 Firefox 中,使用 XMLHttpRequest 效果很好,但在 IE (6-8) 中我收到错误:

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);
...

有什么想法吗?

I am trying to work with xml and javascript. In firefox it works great using XMLHttpRequest but in IE (6-8) I am getting the error:

Object doesn't support this action

I am using the following function:

   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;
}

And then calling it with:

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

Any thoughts??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

熟人话多 2024-08-19 01:36:32

尝试为请求指定一个局部变量,var request(尽管它看起来不应该解决它)。

我会使用这个 fn 来实现轻量级 XHR:

/** XHConn - Simple XMLHTTP Interface - [email protected] - 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);

Try specifying a local variable for request, var request ( although it doesn't look like it should solve it ).

I would use this fn for light-weight XHR:

/** XHConn - Simple XMLHTTP Interface - [email protected] - 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;
}

Usage:

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);
╰ゝ天使的微笑 2024-08-19 01:36:32

我认为你需要在请求前面放一个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经常有全局变量的问题。

I think you need to put a var in front of request:

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 often has problems with global variables.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文