4

I have a page that saves data using jQuery AJAX. On the server side, if the saving process fails, I want to set the StatusDescription of the Response object to "Hey this is Patrick!". The problem is, I can't display the StatusDescription on the client side! It always give me "Internal Server Error". How can I display my custom error message?

Save.ashx

Catch ex As Exception
    Transaction.Rollback()
    context.Response.StatusCode = 500
    context.Response.StatusDescription = "Hey this is Patrick!"
End Try

AJAX

$.ajax
({
    type: 'POST',
    url: 'Save.ashx',
    data: data,
    async: false,
    success: function(Data, TextStatus, XHR) {
        alert(Data);
    },
    error: function(XHR, TextStatus, ErrorThrown) {
        alert(ErrorThrown);
    }
});
kazinix
  • 28,987
  • 33
  • 107
  • 157
  • Internal server error means it is not able to call the server side method. – शेखर Nov 08 '12 at 06:23
  • @krshekhar No, I'm pretty sure it can. I set the status code to 500 that's why it's Internal Server Error. what I want to do is to change that message through StatusDescription property. – kazinix Nov 08 '12 at 06:26
  • Interesting that ASP.NET works like that. With WCF web hosting (`WebServiceHost`), `errorThrown` is the same as `WebOperationContext.Current.OutgoingResponse.StatusDescription`. – Ohad Schneider Apr 06 '14 at 14:35

2 Answers2

1

use XHR.responseText and then see what is the error.
Here is a good link regarding the error of jQuery Call
jQuery Ajax error handling, show custom exception messages

$.ajax
({
    type: 'POST',
    url: 'Save.ashx',
    data: data,
    async: false,
    success: function(Data, TextStatus, XHR) {
       alert(Data);
     },
     error: function(XHR, TextStatus, ErrorThrown) {
       alert(XHR.responseText);
     }
 });
Community
  • 1
  • 1
शेखर
  • 17,412
  • 13
  • 61
  • 117
  • There is an error of course. What I want to do is to set the status description and show it on the client side. It doesn't matter what the error is. – kazinix Nov 08 '12 at 06:28
  • Internal server error it's more like generic error you will need the specific error first. – शेखर Nov 08 '12 at 06:32
  • are you able to call when every thing is ok?? – शेखर Nov 08 '12 at 06:33
  • You misunderstood my goal. I know what the error is, what I want to do is to create a friendly error message for the users, I want to do that by setting `context.Response.StatusDescription`. My problem is, on the client side, I don't know how to retrieve `StatusDescription`. – kazinix Nov 08 '12 at 06:36
  • It will be easy to use responseText instead but I have pages that use `context.Response.StatusDescription`. Why it's not working? – kazinix Nov 08 '12 at 06:41
  • I don't know the reason. But I will look for it. – शेखर Nov 08 '12 at 07:16
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/19264/discussion-between-dpp-and-krshekhar) – kazinix Nov 08 '12 at 07:38
  • sorry chat room is not accessible from my organisation – शेखर Nov 08 '12 at 07:39
0

Instead I would return a string message in the response form the server and then you can use it in your error function.

Abubakkar
  • 15,488
  • 8
  • 55
  • 83