#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    18
    Rep Power
    0

    Ajax sometimes gets a 500 internal server error, how to continue the script?


    Hi,

    I have an AJAX query and sometimes, probably because of server-side problems to which I make a request to, I get an 500 internal server error.

    It isn't the end of the world that this happens every now and then, though I'd like to have it continue the script if this happens; can I do something like:

    if (500 internal server error): call this function

    so that the script at least keeps running?

    Thanks, Dirk
  2. #2
  3. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,041
    Rep Power
    119
    If your using jQuery; you could just check for the statusCode and re-fetch/re-query the url; if you received a 500 statusCode. As an alternate; another route, would be to create a regular expression and search/match/test it up against your responseText and if your server returns, an actual "500 internal server error" page; then re-fetch/re-query the url again.
  4. #3
  5. No Profile Picture
    http://stealthwd.ca
    Devshed Novice (500 - 999 posts)

    Join Date
    Dec 2005
    Posts
    688
    Rep Power
    200
    When it returns the 500 is it doing so in a timely fashion? If so you should be able to handle it like any other response, but check the response to see if its a 500 or not.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    18
    Rep Power
    0
    Originally Posted by web_loone08
    If your using jQuery; you could just check for the statusCode and re-fetch/re-query the url; if you received a 500 statusCode. As an alternate; another route, would be to create a regular expression and search/match/test it up against your responseText and if your server returns, an actual "500 internal server error" page; then re-fetch/re-query the url again.
    Thanks, the statusCode did the trick!

    I also tried the error callback of the AJAX query, however this doesn't work (error: do this, success: do this). Any idea why AJAX doesn't see 500 internal server error as an error? And if it doesn´t see a 500 as an error, what is actually an error for AJAX?
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    18
    Rep Power
    0
    Originally Posted by Dameon51
    When it returns the 500 is it doing so in a timely fashion? If so you should be able to handle it like any other response, but check the response to see if its a 500 or not.
    What do you mean with timely fashion? It is kind of arbitrary when it occurs and it if it occurs I think it takes about 15-20 seconds before it gives the 500 error code back.
  10. #6
  11. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,041
    Rep Power
    119
    Originally Posted by Dirk83
    Thanks, the statusCode did the trick!

    I also tried the error callback of the AJAX query, however this doesn't work (error: do this, success: do this). Any idea why AJAX doesn't see 500 internal server error as an error? And if it doesn´t see a 500 as an error, what is actually an error for AJAX?
    I assume it's because they're now is a specific parameter for handling HTTP status codes. You might want to look into .ajaxError(). It might give you more control over generic based error handling.

IMN logo majestic logo threadwatch logo seochat tools logo