How to handle server sided exceptions with AJax queries on the client end?

It depends on what does the AJAX method return. If it is plain HTML, you could output an error message instead, which will be presented to the user. If it is JSON, then add some fields to the structure, which can hold an error message or error code, so you can handle it when you retrieve the response client side.

– Ali May 26 '10 at 12:15 The best way to handle this is to... catch the exception :) Surround the code that can throw with a try-catch block and fill the JSON errors in the catch. That's the beauty of exceptions: you can catch them and act upon them. – Palantir May 26 '10 at 12:41.

Your ajax request can respond with a server code of 500 alerting the AJAX caller that something went horribly wrong. Most libraries implement an onFailure callback that will be triggered in this event.

I don't know if you use jQuery or not. My answer is given assuming you use jQuery, but you can apply the same concept to another JavaScript framework (or none). In this way, the users would actually be looking at the stack trace instead of a generic error message (which is uselss anyway): $.

Ajax({ url: '/some_url', type: 'post', //or get data: ..., success: function() { /* handle success */ }, error: function(XMLHttpRequest, textStatus, errorThrown) { $('#error_dlg'). Dialog('open'). Trigger('initDlg', XMLHttpRequest); } }); $('#error_dlg').

Dialog({ autoOpen: false, width: 550, height: 400, modal: true, buttons: { 'Close': function() { $(this). Dialog('close'); } }, open: function(event, ui) { } }). Bind('initDlg', function(e, XMLHttpRequest) { $('#error_stack_trace').

Append($(''). Html(XMLHttpRequest. ResponseText)); }).

Something I ran into recently that might be helpful is this: php.net/manual/en/function.set-error-han... Basically, if PHP runs into an error, it will use your handler, which you can design to work with an asynchronous request or a synchronous request. However, it seems that you are also throwing exceptions within your code. From a cursory glance, you can create a custom Exception handling class as well.

w3schools.com/php/php_exception.asp Once again, you can design it to work with asynchronous or synchronous requests. In your Javascript, if you are using JSON as a response, you can simply set an error flag that will alert your code of an exception/error and it will respond appropriately. If you're using straight HTML as a response, you might have to send a 500 error (or one of the other variants, I'm not well versed in the 500 codes) so that you can use the onFailure or failure function handlers as others have mentioned.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions