codejanitor

AJAX, Safari, and Returning an HTTP Error Code

Mar. 30th, 2006

For my AJAX apps I decided to return a 500 error code if there was some sort of problem on the server side (e.g. user doesn’t have permissions, database error, etc.). That way my onFailure code will be called by prototype and the page will reset itself and let the user know what the problem was. In PHP it just requires a header(’HTTP/1.0 500 We Have a Problem Houston’); Even better is that, because the 500 status code is just a header, I can also pass back XML (i.e. with multiple error messages) if I want.

However, I ran into a problem when debugging this in Safari today. Even though I sent back the 500 status code the onSuccess function was being called in Safari. Alerting the request.status and request.getAllResponseHeaders() gave me “undefined.” After a bit of hunting I figured out that it only happend when my PHP script outputted nothing. The solution? Just have PHP print something (a space suffices) before exiting.

Filed under: Jason @ 6:15 pm

5 Responses to “AJAX, Safari, and Returning an HTTP Error Code”

  1. Scott Meves Says:

    Thanks Jason! I was having this problem today, and your post just saved me endless hours of debugging!

  2. Alixe Bhagooa Says:

    Yep, thank you I was having a problem with Safari as well.
    I added echo ” “; //for safari in php script and all work gr8 :)

    Happy to have found this quickly! Thanks Google!

  3. janiv Says:

    Hi
    if I want to catch time out status 304?

  4. Erik Says:

    I’m trying to send an error header like you did, but I also want to send back the error message, how do I do both? The only thing I seem to get back is the error header… Does it stop outputting after the error header is sent?

  5. Jason Says:

    Erik, I know you can send back additional data besides the header so long as you don’t exit after sending the header. Are you echoing XML out in addition to setting the header?

Leave a Reply