I have read a few posts on fail parameters for a JQuery Ajax call, but none that have directly answered my question. If you want to read up on my jumping off point here, this post would be a good start:
jquery: is there a fail handler for $.post in Jquery?
My problem is that there are a handful of things that may cause my application to fail – if my script returns false the above method would work just fine for me (if I understand it correctly), but most of the time my script will fail by kicking out an exception that I handle using the Zend Framework. I would prefer to return the exception so that I can provide the user with a more detailed message. Is it possible to have my PHP script return a value while still letting the Ajax call know that it was a failure?
Sure you can. First of all you need to categorize you errors, for example:
I would advise you to take as a return value for correct and with no errors processing – 0. In all other case that would be an error.
Another useful advise would be to use JSON as a client-server conversation.
In PHP it would be:
In this case you could pass error code and message, and additional params in $extra, for example, for this call:
Response from server side would be:
For the client side you need a wrapper function for $.ajax:
and function to validate JSON, in order if the corrupted format comes.
Wrap in try-catch you code, and in catch statement do something like:
The result would be that you receive debug information in browser console, and could handle errors / exception (prepareJSONResponse()) and fatals (by reading HTTP-status header, if it’s not 200, then there was error).
Hope thats what you asked about.