#1
  1. 300lb Bench!
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Aug 2001
    Location
    New York
    Posts
    2,350
    Rep Power
    61

    How do I fire off a second ajax request if first request fails?


    Right now, I have a request like the following:

    Code:
    $.getJSON("/somepage.php",{id:1},function(json) {
      //Take care of things
    }
    Which is all fine and good when the request is successful and returns a response. Every once and a while, however, the request fails. I believe it's due to network error on the client side.

    What I would like is if the request fails, kill it and create a new request. I see that if I change $.getJSON to $j.ajax, there is a "timeout" option that I can set. It will then fire off the function under "error" if that timeout period passes.

    So here's my question. If the timeout period passes, how do I kill the first request and make a second request? Ideally, I'd like to make two requests and if both of them fail, then maybe let the user know that there are network issues. Thanks in advance.
    Correspondence chess
    nothingbutchess.com
  2. #2
  3. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,301
    Rep Power
    7170
    If the timeout period passes the request should already be dead. You would run the second request in the same way that you ran the first one.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  4. #3
  5. 300lb Bench!
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Aug 2001
    Location
    New York
    Posts
    2,350
    Rep Power
    61
    Seems simple enough. Let me try and I'll let you know how it goes.
    Correspondence chess
    nothingbutchess.com
  6. #4
  7. 300lb Bench!
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Aug 2001
    Location
    New York
    Posts
    2,350
    Rep Power
    61
    Ok, so that seems to have worked out just fine. In the end, I changed $.getJSON to $j.ajax and did

    Code:
    request_number = 1;
    function my_function(param) {
    //Stuff done before hand
              $.ajax({
                cache:false,
                timeout:2000,
                type: 'get',
                url: '/somepage.php',
                dataType: 'json',
                data: 'id=1',            
                error: function() {
                  if (request_number > 1) {
                    //Network issues?
                    $('#network_error')[0].style.display = "inline";
                  } else {
                    request_number++;
                    my_function('');
                  }
                },
                success: function(json) {
                  //run success code
                }
    }

    Obviously I'm simplifying the code here. The only thing I had to do (which you don't see here) is I use request_number to not run code within my_function if it's greater than 1, because that means the function is being called again (due to network issues) and everything within the function before the ajax call doesn't need to be run again.

    I simulated network issues by making somepage.php sleep for 10 seconds, which is longer than the two second time out from my ajax call. I've been testing it and all seems good. Maybe this will help someone else out.
    Correspondence chess
    nothingbutchess.com

IMN logo majestic logo threadwatch logo seochat tools logo