Skip to content Skip to sidebar Skip to footer

JQuery Deferred AJAX & 500 Server Errors

Trying to figure out how to debug generic 500 errors from my .ASMX JS: function TestError() { return $.ajax({ type: 'POST', url: 'Order.asmx/TestError',

Solution 1:

After a kick in the right direction and further digging I actually found this problem was twofold.

First, Dave Ward actually pointed me in the right direction in suggesting I turn off customErrors in the web.config. It was actually not that, but very close.

The actual culprit is the Elmah error handling module. After disabling it I was able to relay custom errors regardless of whether customErrors was on or off.

The second issue was that although I could now handle the errors, I still couldn't pass System.Exception objects. I discovered this is because the Exception.TargetSite property is not serializable.

I got around the problem by creating my own JsonException class and only including the serializable properties of the System.Exception class (abbreviated here for simplicity).

public class JsonException
{
    public string Source { get; set; }
    public string Message { get; set; }
    public string StackTrace { get; set; }

    public JsonException(Exception ex)
    {
        this.Source = ex.Source;
        this.Message = ex.Message;
        this.StackTrace = ex.StackTrace;
    }
}

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public object TestError()
{
    try
    {
        throw new Exception("Testing error");
    }
    catch (Exception ex)
    {
        return new JsonException(ex);
    }
}

And now I finally get the data I want:

enter image description here


Solution 2:

I would change to this:

$.ajax({
        type: "POST",
        url: 'Order.asmx/TestError',
        contentType: "application/json; charset=utf-8",
        error: function (XMLHttpRequest,textStatus,errorThrown){ console.log(errorThrown); }
    });

Post a Comment for "JQuery Deferred AJAX & 500 Server Errors"