Synchronous request in Firefox not working

D

duncansinclair

The following code works in IE, but in Forefox (1.0.7 & 1.5) the
Asyncronous function (testA) works fine but the Synchronous one (testS)
does not. It just doesn't work & I get no errors.

Anybody have an idea why?

<script>
function testA () {
var element = document.getElementById('show1');
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
} else if (window.ActiveXObject) {
req = new ActiveXObject("Microsoft.XMLHTTP");
}
req.onreadystatechange = function() {
if (req.readyState == 4 && req.status == 200) {
element.innerHTML = req.responseText;
}
}
req.open("POST", 'response.php', true);
req.setRequestHeader("Content-type",
"application/x-www-form-urlencoded");
req.send(null)
}
function testS () {
var element = document.getElementById('show2');
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
} else if (window.ActiveXObject) {
req = new ActiveXObject("Microsoft.XMLHTTP");
}
req.onreadystatechange = function() {
if (req.readyState == 4 && req.status == 200) {
element.innerHTML = req.responseText;
}
}
req.open("POST", 'response.php', false);
req.setRequestHeader("Content-type",
"application/x-www-form-urlencoded");
req.send(null)
}
</script>
<span id="show1">show 1</span><br>
<span id="show2">show 2</span><br><br>
<input type="button" value="Test Async" onClick="testA();">
<input type="button" value="Test Sync" onClick="testS();">
 
M

Martin Honnen

The following code works in IE, but in Forefox (1.0.7 & 1.5) the
Asyncronous function (testA) works fine but the Synchronous one (testS)
does not. It just doesn't work & I get no errors.

Anybody have an idea why?
function testS () {
var element = document.getElementById('show2');
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
} else if (window.ActiveXObject) {
req = new ActiveXObject("Microsoft.XMLHTTP");
}
req.onreadystatechange = function() {
if (req.readyState == 4 && req.status == 200) {
element.innerHTML = req.responseText;
}
}
req.open("POST", 'response.php', false);
req.setRequestHeader("Content-type",
"application/x-www-form-urlencoded");
req.send(null)

Don't use onreadystatechange if you want to do synchronous stuff, put
the code to deal with the response after the send call, that synchronous
call is blocking until the response comes and your statements after the
send call are executed and can deal with status, responseText etc.
At least that should solve the problem I think.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top