AJAX won't work with IE, but works with Firefox and Safari

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Jan 2005
Posts: 70
Reputation: Diode is on a distinguished road 
Solved Threads: 0
Diode's Avatar
Diode Diode is offline Offline
Junior Poster in Training

AJAX won't work with IE, but works with Firefox and Safari

 
0
  #1
Sep 2nd, 2008
I don't know what happened to my previous topic about this.

But I am using an AJAX menu on a page I am making for somebody, and I used the exact same code w3schools recommends to use:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. function updatemenu(leagueno, confno, location, mode)
  2. {
  3.  
  4. xmlHttp=GetXmlHttpObject();
  5.  
  6. if (xmlHttp==null)
  7. {
  8. alert ("Your browser does not support AJAX!");
  9. return;
  10. }
  11.  
  12. var url="updatemenu.php";
  13. url=url+"?mode="+mode+"&leagueno="+leagueno+"&confno="+confno+"&loc="+location;
  14. xmlHttp.onreadystatechange=stateChanged;
  15. xmlHttp.open("GET",url,true);
  16. xmlHttp.send(null);
  17. }
  18.  
  19. function stateChanged()
  20. {
  21. if (xmlHttp.readyState==4)
  22. {
  23. document.getElementById("results").innerHTML=xmlHttp.responseText;
  24. }
  25. }
  26.  
  27. function GetXmlHttpObject()
  28. {
  29. var xmlHttp=null;
  30.  
  31. try
  32. {
  33. // Firefox, Opera 8.0+, Safari
  34. xmlHttp=new XMLHttpRequest();
  35. }
  36.  
  37. catch (e)
  38. {
  39. // Internet Explorer
  40.  
  41. try
  42. {
  43. xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  44. }
  45.  
  46. catch (e)
  47. {
  48. xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  49. }
  50. }
  51.  
  52. return xmlHttp;
  53. }

It works flawlessly in Firefox, Safari, etc., (any browser that adheres to standards), but Internet Explorer doesn't load it, and it gives an error. And in fact, it says the error is on line 169, but the page isn't that many lines long, so I can't find where it is. I'm sure it's on statechanged or one of the xmlHttp=new ActiveXObject lines.

But this is the code w3schools says to use, so I don't know why it's not working.

Any ideas?
Thanks
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 70
Reputation: Diode is on a distinguished road 
Solved Threads: 0
Diode's Avatar
Diode Diode is offline Offline
Junior Poster in Training

Re: AJAX won't work with IE, but works with Firefox and Safari

 
0
  #2
Sep 2nd, 2008
It's also worth adding, I searched the topic list for topic relating to this problem, but none of the advice offered solved my problem.

Thanks.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 380
Reputation: langsor is an unknown quantity at this point 
Solved Threads: 33
langsor langsor is offline Offline
Posting Whiz

Re: AJAX won't work with IE, but works with Firefox and Safari

 
0
  #3
Sep 3rd, 2008
Sorry, it works in my IE7 and I don't have any of the legacy IE versions around to test it on.

This is what I have from your code above, after some mild reformatting clean up (to my tastes) :-)

w3c_ajax.php
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <?php
  2. if ( $_REQUEST ) {
  3. print 'test response';
  4. exit;
  5. }
  6. ?>
  7. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  8. <html xmlns="http://www.w3.org/1999/xhtml">
  9. <head>
  10. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  11. <title>Untitled Document</title>
  12. <script type="text/javascript">
  13. function updatemenu( leagueno, confno, location, mode ) {
  14.  
  15. var xmlHttp = GetXmlHttpObject();
  16.  
  17. if ( xmlHttp == null ) {
  18. alert ( "Your browser does not support AJAX!" );
  19. return;
  20. }
  21.  
  22. var url="w3c_ajax.php";
  23. url = url + "?mode=" + mode + "&leagueno=" + leagueno + "&confno=" + confno + "&loc=" + location;
  24. xmlHttp.onreadystatechange = stateChanged;
  25. xmlHttp.open( "GET", url, true );
  26. xmlHttp.send( null );
  27. }
  28.  
  29. function stateChanged () {
  30. if ( this.readyState == 4 ) {
  31. if ( this.status == 200 ) {
  32. alert( this.responseText );
  33. document.getElementById("results").innerHTML = this.responseText;
  34. } else {
  35. alert( 'The server failed to process your request' );
  36. }
  37. }
  38. }
  39.  
  40. function GetXmlHttpObject () {
  41. var xmlHttp = null;
  42. try {
  43. // Firefox, Opera 8.0+, Safari
  44. xmlHttp = new XMLHttpRequest();
  45. } catch (e) {
  46. // Internet Explorer
  47. try {
  48. xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
  49. } catch (e) {
  50. xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  51. }
  52. }
  53. return xmlHttp;
  54. }
  55. </script>
  56. </head>
  57. <body>
  58. <div id="results"></div>
  59. <a href="javascript:" onclick="updatemenu()">Update Menu Test</a>
  60. </body>
  61. </html>

But I didn't really change anything except make xmlHttp local to the function and then use the this keyword in the stateChanged method -- give it a try and see if it works in yours now? If not, what IE version are you using?

...
Google is the answer to all of your questions -- the trick is knowing what question to ask in your specific predicament.
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 70
Reputation: Diode is on a distinguished road 
Solved Threads: 0
Diode's Avatar
Diode Diode is offline Offline
Junior Poster in Training

Re: AJAX won't work with IE, but works with Firefox and Safari

 
0
  #4
Sep 3rd, 2008
IE 5.2 for Mac. I don't have a Windows installation in the house. So I have to use it for Mac, which may be kind of old.

But your example displayed 'text response', so I take it that means it worked, right?

I'll have to view it on his laptop then and see if it works, but I have before, and it always said "Error on such and such line" but it was always a much higher number than the actual number of lines in the php file. And I haven't changed anything since the original copy and paste from w3c's site. But I'll have to check it again.

Thanks for your reply.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 380
Reputation: langsor is an unknown quantity at this point 
Solved Threads: 33
langsor langsor is offline Offline
Posting Whiz

Re: AJAX won't work with IE, but works with Firefox and Safari

 
0
  #5
Sep 3rd, 2008
Originally Posted by Diode View Post
IE 5.2 for Mac. I don't have a Windows installation in the house. So I have to use it for Mac, which may be kind of old.
Wow, when I build websites I just ignore IE for Mac since it's so poorly designed (and no longer supported) it's just not worth the trouble. But hey, if it works in that browser it's bound to work most everywhere else too. :-)

Cheers
Google is the answer to all of your questions -- the trick is knowing what question to ask in your specific predicament.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC