asp.net MVC / jQuery Ajax returning 500 Exception not JSON Result

758 Views Asked by At

I have a asp.net MVC 2 web application in all Middle Tier Controller Actions I catch all exceptions, Log, and then rethrows the Exception. This rethrow is picked up by the JQuery Ajax error handler

throw new Exception(e.Message);

I am making an ajax call via JQuery like this

jQuery.ajax({ url: '/controller/action', 
 success: function(data) {  }, 
 error: function(jqXHR, textStatus, errorThrown) {     
    var error_json = JSON.parse(jqXHR.responseText);  // this is returning error
    var html_error = error_json["Message"]; 
 } 
});

And for some reason jqXHR.responseText is not returning anything anymore and I am getting an error when parsing the responseText. I think it may be a IIS setting since we recently switched to a new webserver. ** Looking into Firebug there is no more JSON response as normal. I just see the server response as a 500 Exception **

2

There are 2 best solutions below

0
On

Looks like it was an IIS 7 setting. In Error Pages --> Edit Feature Settings --> Select Detailed Errors. –

0
On

try add this to web.config. When return JsonResult AND statusCode = 500 AND errorMode != Details, the responseText in the returning json will be "Internal Server Error" in html code instead of json string.

<system.webServer>
    <httpErrors errorMode="Detailed" />
</system.webServer>