jQuery推迟了AJAX和500服务器错误
试图找出如何从我的.ASMX调试generics500错误
JS:
function TestError() { return $.ajax({ type: "POST", url: 'Order.asmx/TestError', contentType: "application/json; charset=utf-8" }); } $.when(TestError()).then(function (err) { console.log(err); });
C#
[WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public object TestError() { try { throw new Exception("Testing error"); } catch (Exception ex) { return ex; } }
结果:
它甚至没有记录err
参数。 有任何想法吗?
编辑
将错误添加到原始Ajax调用是让我记录信息,但仍然是通用和无用的。
http://i42.tinypic.com/2v27fgo.jpg
编辑2
回答了我自己的问题并要求跟进:
在C#中删除inheritance对象的属性
经过正确的方向和进一步的挖掘,我发现这个问题是双重的。
首先, Dave Ward实际上指出了我正确的方向 ,建议我在web.config中关闭customErrors
。 实际上并非如此,但非常接近。
实际的罪魁祸首是Elmaherror handling模块。 禁用它后,无论customErrors
是on
还是off
,我都能够中继自定义错误。
第二个问题是虽然我现在可以处理错误,但我仍然无法传递System.Exception
对象。 我发现这是因为Exception.TargetSite
属性不可序列化 。
我通过创建自己的JsonException
类来解决这个问题,并且只包含System.Exception
类的可序列化属性(为简单起见,这里JsonException
)。
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); } }
现在我终于得到了我想要的数据:
我会改为:
$.ajax({ type: "POST", url: 'Order.asmx/TestError', contentType: "application/json; charset=utf-8", error: function (XMLHttpRequest,textStatus,errorThrown){ console.log(errorThrown); } });