如何指定消息在ajax调用c# webmethod抛出异常
How to specify message on ajax call to C# webmethod that throws an exception?
我有一个c# web方法,当调用超时(>30秒)时,有时会抛出异常。这很好,我期望这种行为,但问题是,当ajax调用遇到.fail回调时,错误消息表明"内部服务器错误"。我希望能够捕获异常并报告数据库超时。我该怎么做呢?
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static string GetSPResults(string reportId, string sproc, Dictionary<string, string> parameters, string[] ensureArrays, string[] encryptArrays, string[] dateFields)
{
...
XElement result = avdata.ExecuteSPXmlXElement(sproc, parameters, null);
...
}
$.ajax({
type: "POST",
url: "/Patrol/Report.aspx/GetSPResults",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: JSON.stringify(postData)
}).done(function (result) {
...
}).fail(function (jqXHR, textStatus, err) {
alert("An error has occurred: " + err); //rerpots "Internal server error" no matter what problem occurs server-side
});
我用这个
$.ajax({
type: "POST",
url: "/Patrol/Report.aspx/GetSPResults",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: JSON.stringify(postData)
error: function (jqXHR, textStatus, errorThrown) {
mensaje = false;
if (jqXHR.status === 0) {
alert("Not connect: Verify Network.");
} else if (jqXHR.status == 404) {
alert("Requested page not found [404]");
} else if (jqXHR.status == 500) {
alert("Internal Server Error [500].");
} else if (textStatus === 'parsererror') {
alert("Requested JSON parse failed.");
} else if (textStatus === 'timeout') {
alert("Time out error.");
} else if (textStatus === 'abort') {
alert("Ajax request aborted.");
} else {
toastr.error("Uncaught Error:", "Mensaje Servidor");
}
}
});
相关文章:
- 为什么浏览器没有为语法错误抛出异常
- PHP REST服务抛出异常——希望避免控制台错误
- 测试抛出异常的函数
- (为什么)我不能从生成器中抛出异常吗
- 如何抛出异常
- 抛出异常:Node.js与Gevent
- 当请求DOM对象时,XMLHttpRequest.send()抛出异常
- AngularJS在尝试轻松保存Changes时抛出异常
- $mdDialog.prompt 抛出异常,但该函数在确认时工作正常
- 在 Javascript 中,即使从未抛出异常,使用 try-catch 块是否昂贵
- 自定义抛出异常JavaScript的错误消息
- 在网格弹出编辑器中上传剑道:如何防止上传抛出异常时窗口关闭
- 在原型方法中抛出异常,未在外部尝试捕获 (JavaScript) 中捕获
- XSLT 2.0 错误处理:向 JavaScript 抛出异常
- 为什么抛出异常后不再被视为已发生
- 对于角度为2的循环抛出异常
- Angular Service在返回promise时抛出异常
- mark.setDragable(false)在API谷歌地图的main.js中抛出异常
- Knockoutjs 在尝试创建一系列无序列表时抛出异常
- 如何指定消息在ajax调用c# webmethod抛出异常