IE7 and XMLHttpRequest

N

nrocha

Hi,

I've detected a strange behaviour in my application. I'm calling a
server page through XMLHttpRequest on the onbeforeunload event.
In firefox this works great.
In IE7 the server page only gets called the first time the page is
loaded...
Here is the javascript code:

window.onbeforeunload = unlock;

function unlock()
{
var xmlHttp = null;
try {
xmlHttp = new XMLHttpRequest();
}
catch (e)
{
try {
xmlHttp = new ActiveXObject('Msxml2.XMLHTTP');
}
catch (e)
{
try {
xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
}
catch (e)
{
window.alert('Your browser does not support AJAX!');
}
}
}
try {
xmlHttp.open('GET','/DocumentationFramework/Syncronize.aspx?
action=unlock',false);
xmlHttp.send(null);
}
catch (e)
{
window.alert(e);
}
}

I have a breakpoint in the PageLoad method of Syncronize.aspx and it
only gets called the first time the window is loaded. If I close the
tab/window and open it again, the next call will be as expected, but
from there it won't work...the javascript method gets called, but the
server page don't...

Before anyone asks, I've put "false" in xmlHttp.open method because I
want to wait for the call to return.

Any sugestions?

Regards,
Nuno
 
J

Jani Tiainen

nrocha kirjoitti:
Hi,

I have a breakpoint in the PageLoad method of Syncronize.aspx and it
only gets called the first time the window is loaded. If I close the
tab/window and open it again, the next call will be as expected, but
from there it won't work...the javascript method gets called, but the
server page don't...

Before anyone asks, I've put "false" in xmlHttp.open method because I
want to wait for the call to return.

Any sugestions?


Sounds like you've hit IE XMLHttpRequest caching "problem". IE is stupid
(?) to cache requests and if you ask same URL again and again it will
return it from a cache. So you need to put something to generate unique
URL everytime, timestamp for example.

Or "bug" in FF, which way you want to put it.

But neither browsers don't work correctly.

So IE caches your request always but doesn't check for freshness - so
same URL returns always cached copy.

FF doesn't cache anything but requests page everytime from server - and
causes unnecessary traffic.
 
N

nrocha

nrocha kirjoitti:





Sounds like you've hit IE XMLHttpRequest caching "problem". IE is stupid
(?) to cache requests and if you ask same URL again and again it will
return it from a cache. So you need to put something to generate unique
URL everytime, timestamp for example.

Or "bug" in FF, which way you want to put it.

But neither browsers don't work correctly.

So IE caches your request always but doesn't check for freshness - so
same URL returns always cached copy.

FF doesn't cache anything but requests page everytime from server - and
causes unnecessary traffic.

Problem solved.
Sending an POST request instead of a GET request does the trick to
force a refresh. I haven't tried the timestamp with GET request
approach...
Here's the new code for anyone interested:

var params = "action=" + action;
xmlHttp.open('POST','/DocumentationFramework/Syncronize.aspx',false);
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-
urlencoded");
xmlHttp.setRequestHeader("Content-length", params.length);
xmlHttp.setRequestHeader("Connection", "close");
xmlHttp.send(params);

Regards,
Nuno
 

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

Similar Threads


Members online

Forum statistics

Threads
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top