It is currently Sun Dec 17, 2017 2:18 am



Reply to topic  [ 2 posts ] 
reusing xmlhttprequest objects in IE 
Author Message
Felix Rex
User avatar

Joined: Fri Mar 28, 2003 6:01 pm
Posts: 15728
Location: On a slope
Reply with quote
Post reusing xmlhttprequest objects in IE
Ok, just ran into this so I thought I'd jot it down.

Basically, I have some ajax code I threw together to be able to reuse xmlhttprequest objects. That way I'm not constantly creating new ones and chewing up memory/time.

I built most of this in FF and it worked great, since I can use firebug to debug js issues. Tested it in IE7 and the first request worked... then the second request did nothing. I debugged it pretty heavily and found I was sending the ajax request on the second attempt, but the onreadystatechange event handler was never firing. I was very, very confused.

I did learn some neat things in the process, but it turns out in IE you need to define your onreadystatechange event handler AFTER you call the XMLHttpRequest.open method. For other reason, you also have to define it before you call the XMLHttpRequest.send method. My problem is I was defining the even handler before the open.

In the end I ended up with code similar to this (trimmed for brevity)

Code:
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
   try { xmlhttp = new XMLHttpRequest(); } catch (e) { xmlhttp = false; }
}
if(!xmlhttp)
   return false;
xmlhttp.open('POST', that.page, true);
xmlhttp.onreadystatechange = function(){
   if(xmlhttp.readyState == 4){
      if(xmlhttp.status == 500){
         that.showError('Internal Server Error');
         return;
      }
      try{ var result = xmlhttp.responseXML; }
      catch(e){   that.showError('Result not readable', result);   that.processing = false; return;}
      try{ var errors = result.getElementsByTagName('error');}
      catch(e){   that.processing = false; return; }
      if(errors.length > 0){
         var error = '';
         for(i=0; i<errors.length; i++){ error += errors.item(i).firstChild.nodeValue + '<br>';   }
         that.showError(error, result);
         that.processing = false;
         return;
      }
      try{ var response = result.getElementsByTagName('responseBlock'); }
      catch(e){   that.showError('AJAX response tag missing or unreadable'); that.processing = false; return; }
      ajaxResponse(response);
      xmlhttp.readyState = 0;
   }
}
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-length", vars.length);
xmlhttp.setRequestHeader("Connection", "close");
xmlhttp.send(vars);   


Tue Sep 01, 2009 12:46 pm
Profile WWW
Felix Rex
User avatar

Joined: Fri Mar 28, 2003 6:01 pm
Posts: 15728
Location: On a slope
Reply with quote
Post 
for more information, check the bottom of this page:

http://en.wikibooks.org/wiki/XMLHttpRequest


Tue Sep 01, 2009 12:49 pm
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 2 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware.