DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   JavaScript / DHTML / AJAX (http://www.daniweb.com/forums/forum117.html)
-   -   AJAX won't work with IE, but works with Firefox and Safari (http://www.daniweb.com/forums/thread143616.html)

Diode Sep 2nd, 2008 1:46 pm
AJAX won't work with IE, but works with Firefox and Safari
 
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:

function updatemenu(leagueno, confno, location, mode)
  {

    xmlHttp=GetXmlHttpObject();

    if (xmlHttp==null)
      {
        alert ("Your browser does not support AJAX!");
        return;
      }

    var url="updatemenu.php";
    url=url+"?mode="+mode+"&leagueno="+leagueno+"&confno="+confno+"&loc="+location;
    xmlHttp.onreadystatechange=stateChanged;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
  }

function stateChanged()
  {
    if (xmlHttp.readyState==4)
      {
        document.getElementById("results").innerHTML=xmlHttp.responseText;
      }
  }

function GetXmlHttpObject()
  {
    var xmlHttp=null;

    try
      {
        // Firefox, Opera 8.0+, Safari
        xmlHttp=new XMLHttpRequest();
      }

    catch (e)
      {
        // Internet Explorer

        try
          {
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
          }

        catch (e)
          {
            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
          }
      }

    return xmlHttp;
  }

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

Diode Sep 2nd, 2008 1:48 pm
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.

langsor Sep 3rd, 2008 2:29 am
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
<?php
if ( $_REQUEST ) {
  print 'test response';
  exit;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
function updatemenu( leagueno, confno, location, mode ) {

  var xmlHttp = GetXmlHttpObject();

  if ( xmlHttp == null ) {
      alert ( "Your browser does not support AJAX!" );
      return;
  }

  var url="w3c_ajax.php";
  url = url + "?mode=" + mode + "&leagueno=" + leagueno + "&confno=" + confno + "&loc=" + location;
  xmlHttp.onreadystatechange = stateChanged;
  xmlHttp.open( "GET", url, true );
  xmlHttp.send( null );
}

function stateChanged () {
  if ( this.readyState == 4 ) {
    if ( this.status == 200 ) {
      alert( this.responseText );
      document.getElementById("results").innerHTML = this.responseText;
    } else {
      alert( 'The server failed to process your request' );
    }
  }
}

function GetXmlHttpObject () {
  var xmlHttp = null;
  try {
    // Firefox, Opera 8.0+, Safari
    xmlHttp = new XMLHttpRequest();
  } catch (e) {
    // Internet Explorer
    try {
      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
  return xmlHttp;
}
</script>
</head>
<body>
  <div id="results"></div>
  <a href="javascript:" onclick="updatemenu()">Update Menu Test</a>
</body>
</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?

...

Diode Sep 3rd, 2008 11:33 am
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.

langsor Sep 3rd, 2008 11:58 am
Re: AJAX won't work with IE, but works with Firefox and Safari
 
Quote:

Originally Posted by Diode (Post 683626)
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


All times are GMT -4. The time now is 1:42 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC