Page 1 of 1

AJAX not working in IE after 1st use

Posted: Sat Jan 06, 2007 4:10 am
by anjanesh
I have this single variable for XMLHttpRequest

Code: Select all

var req = false;

// branch for native XMLHttpRequest object
if (window.XMLHttpRequest)
 {
        try { req = new XMLHttpRequest(); }
        catch(e) { req = false; }
 }
// branch for IE/Windows ActiveX version
else if (window.ActiveXObject)
 {
        try { req = new ActiveXObject("Msxml2.XMLHTTP"); }
        catch(e)
         {
                try { req = new ActiveXObject("Microsoft.XMLHTTP"); }
                catch(e) { req = false; }
         }
 }

req.overrideMimeType('text/xml');
This works fine in FF and IE.
But, in IE, it works fine for the first time, but the next time I make use of AJAX nothing happens.

Code: Select all

req.onreadystatechange = req_foo;

function req_foo()
 {
        if (req.readyState == 1 || req.readyState == 2 || req.readyState == 3) // Open or Send or Receiving
         {
                // ..Loading..
         }
        if (req.readyState == 4) // Loaded
         {
                // only if "OK"
                if (req.status == 200)
                 {
                        // Pull Remote data
                 }
         }
 }
I find it strange that in IE7, it works once but when I click again, nothing happens .
Im guessing I have to reset the req variable after the first use - that is, in if (req.status == 200) block ?

Is there something Im missing here ?

Thanks