SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    Non-Member
    Join Date
    May 2006
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question AJAX: XMLHttpRequest Doesn't Always Go Through

    I have programmed a game in AJAX that checks the server every 2 seconds with XMLHttpRequest for new data. The updating part works fine, but sometimes, when a user presses an options button to perform an action, everything in the function on the button click works, but the http request doesn't go through so the action is never executed. I am wondering if anyone knows why this is happening. Could it possibly be that the http request that happens every 2 seconds overlaps the one made from the button press and keeps the button's request from going through?

    Code:
    function ajax_request2(url) {
    try {
      http_request2 = new XMLHttpRequest();
    } catch (trymicrosoft) {
      try {
        http_request2 = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (othermicrosoft) {
        try {
          http_request2 = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (failed) {
          http_request2 = false;
        }
      }
    }
    
    if(!http_request2) {
    alert('Error: Connection failed.');
    return false;
    }
    http_request2.onreadystatechange=alertContents2;
    http_request2.open('GET',url,true);
    http_request2.send(null);
    }
    
    function alertContents2() {
    	if(http_request2.readyState==4) {
    		if(http_request2.status==200){
    			rec_content(http_request2.responseText);
    		} else { alert("HTTP Request Failed"); }
    	} else { alert("HTTP Request Failed"); }
    }

  2. #2
    SitePoint Member
    Join Date
    Aug 2003
    Location
    California
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it's possible that the browser has cached a response to a request made to the same url with the same arguments. If this is the case, you can do a couple of things:

    - send http headers with your response that will prevent caching
    - add a unique argument to the end of the URL (i.e. the timestamp) so that it will not be cached

    just a thought...

  3. #3
    Non-Member
    Join Date
    May 2006
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you, I will try that.

  4. #4
    Non-Member
    Join Date
    May 2006
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you very much, that solved the problem perfectly


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •