Help! After 24 hours of trying everything, I know a lot more, but the problem persists. Using a simple test program I’ve narrowed down to the fact that with Firefox 3.5.3 and Safari, both on Mac, my xmlHttpRequest works, but only in synchronous mode. If I enable the .onreadystatechange listener, then the callback function seems to catch readyState = 1 and not to run again after that, even though when I disable it (again in synchronous only) readyState moves to 4 and I get the desired responseText.
Why can’t I use asynchronous, and why doesn’t onreadystatechange work?
Thanks! Hope someone can help.
Here is my simplified, but working code:
<html>
<head>
<script type=“text/javascript”>
function myAjax(url)
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject(“Microsoft.XMLHTTP”);
}
xmlhttp.open(“GET”,url,false); // Works only with synchronous httpRequest.
//xmlhttp.onreadystatechange = listener(); // Listener disabled…
// Doesn’t seem to get called after the first time, when readyState = 1.
xmlhttp.send(null);
// Posting response from server on html page.
document.getElementById(‘alpha’).innerHTML=xmlhttp.responseText;
document.getElementById(‘beta’).innerHTML=xmlhttp.readyState;
// readyState = 4 as long as onreadystatechange is disabled and synchronous.
}
// Would be called if onreadystatechange enabled, but is not called here.
function listener(){
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
alert (‘success’);
}
else {
alert (‘another failure’);
} }
</script>
</head>
<body>
<div id=“alpha”>
<p>Line to show server response</p>
</div>
<div id=“beta”>
<p>Line to show readyState</p>
</div>
<button type=“button” onclick=“myAjax(‘serverscript.php’)”>Click php</button>
<button type=“button” onclick=“myAjax(‘xmltext.txt’)”>Click xml</button>
</body>
<!-- **** This is the server side script in the file “serverscript.php”****
<?php
echo ‘This text comes form a php server script.’;
?>
–>
</html>