我有一个jQuery AJAX调用,如下所示:
$.get(url, { tripName: tripName, userRowId: userRowId }, function (data) {
$("#partialView").html(data);
}).success(function () {
UpdateCount();
$("#LocationList").focus();
}).fail(function (error) {
alertify.alert("Call to get sightings failed: " + error);
});
.get调用C#控制器方法。我已经将C#方法设置为抛出零除异常以进行测试。如何捕获来自C#方法的异常消息?我上面的内容不起作用(调用了fail方法,但未显示任何消息),这并不奇怪,因为我只是刺了一下它。
我的C#方法如下所示:
try
{
int a = 0, b = 10, c;
c = b / a;
}
catch (System.Exception exception)
{
throw;
}
改变这个:
alertify.alert("Call to get sightings failed: " + error);
对此:
alertify.alert("Call to get sightings failed: " + error.responseText);
如果您可以在其中设置一个,console.log(error)
则可以看到错误消息被保留,responseText
因此应该可以解决error.responseText
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句