views:

341

answers:

2

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??

A: 

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.

Caleb
hmm still no luck
kilrizzy
+1  A: 

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);
meder
I would change the order of try catch to try `new XMLHttpRequest()` first. IE7+ has native support for XMLHttp object without resorting to ActiveX.
Chetan Sastry
This seems to output the data correctly, and I plugged it in with the rest of my code fine. I am receiving the same error now though when I attempt to loop through an array of elements:var deals = xmlDoc.getElementsByTagName("deal");for( var i in deals){...Does IE not see this as an array?
kilrizzy
Not my function, I would personally too but I was too lazy to update it.
meder
You shouldn't be doing `for var i in deals` because you can iterate through prototypal properties which you dont need to, use .length and a regular vanilla for loop. Please paste the full code too.
meder
`alert( deals.length );` before looping through it, make sure it has a length.
meder
Odd, I changed the loop and in IE I am getting an output of 3 for the length, it alerts the first okay but then gets stuck with an error on the next. Here is where I am working on the file: http://vlmclient.com/dotd/dotd.html
kilrizzy
It seemed to be a: if(deals[i].getAttribute) throwing it off in IE, I removed it since it shouldn't be an issue. Thanks for your help!!
kilrizzy