943,752 Members | Top Members by Rank

Ad:
Sep 2nd, 2008
0

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

Expand Post »
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
Similar Threads
Reputation Points: 50
Solved Threads: 0
Junior Poster in Training
Diode is offline Offline
70 posts
since Jan 2005
Sep 2nd, 2008
0

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

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.
Reputation Points: 50
Solved Threads: 0
Junior Poster in Training
Diode is offline Offline
70 posts
since Jan 2005
Sep 3rd, 2008
0

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

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?

...
Reputation Points: 30
Solved Threads: 36
Posting Whiz
langsor is offline Offline
389 posts
since Aug 2008
Sep 3rd, 2008
0

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

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.
Reputation Points: 50
Solved Threads: 0
Junior Poster in Training
Diode is offline Offline
70 posts
since Jan 2005
Sep 3rd, 2008
0

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

Click to Expand / Collapse  Quote originally posted by Diode ...
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
Reputation Points: 30
Solved Threads: 36
Posting Whiz
langsor is offline Offline
389 posts
since Aug 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Replace Chars in String Problem
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: How can i go about this?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC