IE7 and XMLHttpRequest

Discussion in 'Javascript' started by nrocha, Aug 3, 2007.

  1. nrocha

    nrocha Guest

    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
    nrocha, Aug 3, 2007
    #1
    1. Advertising

  2. nrocha

    Jani Tiainen Guest

    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.

    --

    Jani Tiainen
    Jani Tiainen, Aug 3, 2007
    #2
    1. Advertising

  3. nrocha

    nrocha Guest

    On Aug 3, 12:43 pm, Jani Tiainen <> wrote:
    > 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.
    >
    > --
    >
    > Jani Tiainen


    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
    nrocha, Aug 3, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeff Louella

    IE7 on XP vs IE7 on Vista

    Jeff Louella, Mar 1, 2007, in forum: HTML
    Replies:
    9
    Views:
    502
    Adrienne Boswell
    Mar 2, 2007
  2. John Hosking
    Replies:
    0
    Views:
    400
    John Hosking
    Jan 26, 2009
  3. John Byrne

    IE7 XMLHTTPREQUEST reponseXML

    John Byrne, Nov 23, 2006, in forum: ASP General
    Replies:
    4
    Views:
    138
    Patrice
    Nov 23, 2006
  4. Terry
    Replies:
    1
    Views:
    111
    Terry
    Oct 20, 2007
  5. Jean-Fabrice RABAUTE
    Replies:
    0
    Views:
    99
    Jean-Fabrice RABAUTE
    Mar 20, 2008
Loading...

Share This Page