As I usually see, this is done so that you allow the use of HTTP response codes for things related to the HTTP protocol, and not for your application errors. Then from your HTTP response, you return JSON, which indicates success or failure, as well as any returned data. For example, you can return this:
{ statusCode: 0, // 0 = successful, negative value = error code statusMsg: "success", // you can put any human readable status msg here data: { confirmationMsg: "Data sent successfully." } }
Then your code, which looks at the answer, first looks at the statusCode and depending on whether it is successful or not, it looks for other data.
If you can imagine how this answer will be used by the caller, I find it useful to have different code paths for application-level errors and transport-level errors. If you are thinking of calling jQuery.ajax() . A success handler will be called when the http status code is a successful code. An error handler will be called when the http status code is not successful. That way, you can have common error handlers for all transport-level errors with one ajax error handler, and then you can put your application-level error handling in a success handler by looking at the application-level status code in the returned JSON. If you go to error 400, then you need to program a whole error handler for each ajax call to handle both transfer errors and application-level errors.
jfriend00
source share