下面是我的函数(第一个代码片段),当我运行下面的函数时,它通过我的处理程序进入我的代码,在我的代码中它抛出异常(第二个代码片段),异常被捕获后,它返回到我的处理程序和 get 在 catch 区域内,最后返回到 javascript 成功函数,但错误它没有运行错误部分。
BenefitOperations.performBenefitOperation = function(data) {
$.ajax({
type: "POST",
url: "BenefitOperation.axd",
data: JSON.stringify(data.BenefitOperationJson),
dataType: "json",
contentType: "application/json; charset=utf-8",
beforeSend: function() { PageMask.show(); },
success: function(response) {
if (response.Success == true)
performPostBack();
else
window.alert(Res.BenefitOperationFailure);
},
error: function(e, x, y) { window.alert(Res.BenefitOperationError + y); } }); }
这是我的功能
else
{
throw new ApplicationException(string.Format("Benefit operation type {0} for benefit type {1} is not registered", Enum.GetName(typeof(EmployeeBenefitData.BenefitOperationType), parameters.OperationTypeID), Enum.GetName(typeof(EmployeeBenefitData.BenefitTypeEnum), parameters.BenefitTypeID)));
}
这是我的经理的收获
catch
{
jsonOutput = JsonConvert.SerializeObject(new
{
Success = false
});
}
finally
{
context.Response.Clear();
context.Response.ContentEncoding = Encoding.UTF8;
context.Response.ContentType = "application/json";
context.Response.Write(jsonOutput);
}
最佳答案
$.ajax()
的error
回调不会在您的代码中被调用,因为没有错误。 error
表示检索响应时出现问题,例如。来自服务器的 500 错误。
在您的代码中,您自己捕获了 ApplicationException
并返回了 JSON。如果您更愿意使用 error
处理程序,请引发异常并且不要在您的 C# 代码中捕获它 - 但应该注意您当前的代码是更好的方法。
关于c# - jquery Ajax错误函数未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18848026/