Custom ajax request response

The jquery ajax function has an error function which is triggered when an error happens in the server upon the request been made.

Is there anyway this function give us info(custom) about the event that led to the error,server-side.

For example…I want the function to inform me if the error occurred during an update or an addition to the DB.And i want this info go back to the client so I can manipulate the DOM accordingly.

Only if the response provides appropriate information. A good start would be an error message, if the HTTP status code is not sufficient. but you can also provide in-depth error info.

Of course the response has to be the source of information here…how I can inform that a DB update specifically was made…I suppose that code in server-side will do this…but how this will be shown the response?

It’s your server script, so you can let it return whatever you see fit.

This is… not really Javascript at this point, it’s more server-side language. But for example, have PHP set the [fphp]http_response_code[/fphp] to… 540 and echo out the error message. (540 is an unused status int)
the AJAX call will call the error function (because it received anything other than a 2xx range message). Note that the error function of a jquery AJAX call takes 3 parameters. Your text message will be in the .responseText property of the first parameter (which is a jqXHR).

I think you could register an interceptor in jQuery for every AJAX request and include that hash in every outbound request. On the back-end override the error handler to include that hash with each log write. Otherwise you would include some type of error code in the response that can be identified on front-end and behavior changed accordingly. Http response codes aren’t meant to be specific to an application but that doesn’t mean you can’t return a 404 then provide a response in the body of the request that provides more application specific information for the failure.

that seems a good solution…

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.