不确定bext方法是否向客户端显示异常(例如db异常)(ajax调用)?我应该在下面的控制器中放置一个try / catch吗?目前,我的ajax调用有一个通用异常吗?
控制器:
// GET: Agency Grants
public JsonResult GetAgencyGrants(int agencyID)
{
AuditDAL ad = new AuditDAL();
var grants = ad.GetAgencyGrants(agencyID); //exception here
return this.Json(grants.ToList());
}
Ajax电话:
function GetAuditTypes(container) {
$.ajax({
url: '/AMS/Audit/GetAuditTypes',
type: 'post',
success: function (result) {
$.each(result, function (i, item) {
container.append($('<option/>').text(result[i].Audit_Type_Desc).attr('value', result[i].Audit_Type_ID));
});
},
error: function (xhr, err) {
alert("GetAuditTypes not returned: " + formatErrorMessage(xhr, err));
}
});
}
格式化功能:
// formats errors returned
function formatErrorMessage(jqXHR, exception) {
if (jqXHR.status === 0) {
return ('Not connected.\nPlease verify your network connection.');
} else if (jqXHR.status == 404) {
return ('The requested page not found. [404]');
} else if (jqXHR.status == 500) {
return ('Internal Server Error [500].');
} else if (exception === 'parsererror') {
return ('Requested JSON parse failed.');
} else if (exception === 'timeout') {
return ('Time out error.');
} else if (exception === 'abort') {
return ('Ajax request aborted.');
} else {
return ('Uncaught Error.\n' + jqXHR.responseText);
}
}
我一直收到内部服务器错误,而不是缺少存储过程(真正的异常)。
答案 0 :(得分:0)
ajax error
回调函数处理http
错误(但不是特定的服务器错误)。例如,它不会显示专门的错误消息。像这样更改你的代码:
public JsonResult GetAgencyGrants(int agencyID)
{
AuditDAL ad = new AuditDAL();
try
{
var grants = ad.GetAgencyGrants(agencyID); //exception here
return this.Json(grants.ToList());
}
catch(Exception e){
return this.Json(e.Message);
}
}
并将您的javascript代码更改为:
$.ajax({
url: '/AMS/Audit/GetAuditTypes',
type: 'post',
success: function (result) {
if (result instanceof Array)
{
$.each(result, function (i, item) {
container.append($('<option/>').text(result[i].Audit_Type_Desc).attr('value', result[i].Audit_Type_ID));
});
}
else{
// Exception Occurred
// do somethings with result(contains error message)
}
}
});