@#$% IE (AJAX and Java)

Discussion in 'Web Design and Development' started by yg17, Jun 13, 2006.

  1. yg17 macrumors G5

    yg17

    Joined:
    Aug 1, 2004
    Location:
    St. Louis, MO
    #1
    So, the basics of what I'm attempting to accomplish, is the user submits a form, using AJAX, it's updated, and a row is added to the table. Works great in all browsers, Microsoft's piece of crap included.

    After the form is submitted, and the table is updated, a balance that is elsewhere on the page needs to also be updated. It's basically kinda like a bank register. Add a transaction, the balance is automatically calculated. So, I have this code:

    Code:
    function updateBalance(account) {
        reqBal = openAjax();
        var url = "ajax.php?act=balance&id="+account;
        reqBal.open("get", url, true);
        reqBal.onreadystatechange = handleBalance;
        reqBal.send('null');
        
    }
    
    function handleBalance() {
        if(reqBal.readyState == 4){
            alert(reqBal.responseText);
            document.getElementById('balspan').innerHTML = reqBal.responseText;
            
        }
    }
    updateBalance() is called from the function that submits the stuff to the PHP script to be added to the DB. openAjax() returns an XMLHTTPRequest (and the IE equivalent). Ajax.php calculates the new balance and does nothing else but print out the balance formatted with currency symbol and decimals, and this is captured by reqBal.responseText, as you would expect. This code works perfectly in all Mac browsers. So, I fire up my PC, launch IE and test. The very first time the balance needs to be updated, it works. After that, IE refuses to change the value of responseText next time the function is called....it's basically stuck on the first value it gets when it's first called. Any future calls it ignores and thus, the balance doesn't update. The only way to make it take a new value is to completely close all of IE's windows, open it back up and do it again. It will get a new value, then get stuck on that.

    So, can someone tell me what I screwed up, or some other obnoxious, lengthy, complicated work around to make IE work as it should? Because right now, if IE had them, I'd kick it square in the balls. Thanks
     
  2. bootedbear macrumors 6502

    bootedbear

    Joined:
    Sep 13, 2004
    Location:
    Austin, TX
    #2
    Does it work as expected in other Windows browsers?

    (And where's the Java part? Did you mean Javascript?)
     
  3. yg17 thread starter macrumors G5

    yg17

    Joined:
    Aug 1, 2004
    Location:
    St. Louis, MO
    #3

    Yeah, I meant JavaScript. It's been a long night.

    Haven't tested it in other Windows browsers yet. Works fine in FireFox for OSX, so my guess would be it would work fine in FF for Windows, but I'll eventually test it there to make sure
     
  4. yg17 thread starter macrumors G5

    yg17

    Joined:
    Aug 1, 2004
    Location:
    St. Louis, MO
    #4
    Found the problem. IE was caching the result it got from AJAX, so instead of requesting the page again, it would go to the cache and insert that into responseText, thus the reason for it not changing unless I closed the browser (or clearing temp files i guess). I fixed it by adding a random number to the end of the query string in var url = "ajax.php?act=balance&id="+account; So as long as Javascript doesn't generate the same number twice in a browser session (and it's set to generate between one and 1,000,000,000) then it's OK.

    Really dumb on Microsoft's part. Caching the result defeats the whole purpose of AJAX.
     
  5. bootedbear macrumors 6502

    bootedbear

    Joined:
    Sep 13, 2004
    Location:
    Austin, TX
    #5
    And you're surprised? :cool:

    Caching would have been my guess had you tried it successfully in other Windows browsers. If it had not worked in other browsers then it would probably have been a setup issue.
     
  6. Wes macrumors 68020

    Wes

    Joined:
    Jun 22, 2001
    Location:
    London
    #6
    I don't really know much about AJAX but rather than do a nasty hack with random numbers why don't you append this value you are wanting to update to the end of the query? That way if it changes, it won't use the cache, and if the number is the same it won't matter if it uses the cache?

    Sorry if this doesn't make sense though, might have misinterpreted the question.
     
  7. yg17 thread starter macrumors G5

    yg17

    Joined:
    Aug 1, 2004
    Location:
    St. Louis, MO
    #7
    It's not sending a value to PHP to update a database, it's receiving a value from PHP to update the page.
     
  8. Knox Administrator

    Knox

    Staff Member

    Joined:
    Jul 1, 2002
    Location:
    UK
    #8
    I don't remember having any problems with caching when I was writing MacRumorsLive, as long as the correct HTTP headers were set. However, it is using randomised URLs because Safari 1.2 (I think it was 1.2 anyway) refused to do more than one request on the same URL within the same page load...
     
  9. angelneo macrumors 68000

    Joined:
    Jun 13, 2004
    Location:
    afk
    #9
    I have the same problem with caching on Flash with XML loading over SSL, I managed to resolved that using the following php header, not sure if this will help in this case.

    Pragma:no-cache
    Cache-control:no-cache
     

Share This Page