开发者

Webpages on Firefox works well but no on IE

I have following code snippet:

self.xmlHttpReq = new XMLHttpRequest();

  self.xmlHttpReq.onreadystatechange = function()
  {
    if(self.xmlHttpReq.readyState == 4 && self.xmlHttpReq.status == 200)
    {
      xmlDoc = self.xmlHttpReq.responseXML;
      var xmlVar1 = xmlDoc.getElementsByTagName('var1')[0].childNodes[0].nodeValue;
      var xmlVar2 = xmlDoc.getElem开发者_开发知识库entsByTagName('var2')[0].childNodes[0].nodeValue;
    }
  }

In IE the error code says:

object required, ajax request.js line num, char num

However, this same ajax request works fine in Firefox.


IE and Firefox have different object names for the XMLHttpRequest, you have to check your browser and declare the new object based on that.

Try something like this:

function getXHR() {
    var xhr = false;
    if (window.XMLHttpRequest) {
        xhr = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        try {
            xhr = new ActiveXObject("msxml2.XMLHTTP");
        } catch(e) {
            try {
                xhr = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {
                xhr = false;
            }
        }
    }
    return xhr;
}

I got this from Jeremy Keith some time ago, it has never failed me.


Internet Explorer doesn't have the XMLHttpRequest object. Instead it uses an ActiveX object for the same functionality. So, you need to change this line:

self.xmlHttpReq = new XMLHttpRequest();

to:

if (window.ActiveXObject) {
    try {
        self.xmlHttpReq = new ActiveXObject('Microsoft.XMLHTTP');
    }
    catch (e) {
        self.xmlHttpReq = new ActiveXObject('Msxml2.XMLHTTP'); // for really old versions of IE. You can leave the try/catch out if you don't care to support browsers from the '90s.
    }
}
else
    self.xmlHttpReq = new XMLHttpRequest();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜