我想在data-ajax-failure
使用不显眼的 ajax 的表单部分中捕获我的错误消息。如果表单是通过调用的函数发布的,则很容易做到这一点,$ajax
但我没有这样做,我将成功和失败函数调用都放在表单本身中:
<form method="post" data-ajax-url="/Home/Process_Form" data-ajax="true" data-ajax-method="post" data-ajax-success="form_data_success" data-ajax-failure="form_data_fail">
表单发布到一个名为的控制器操作Process_Form
public IActionResult Process_Charterer(Charterer model) {
if (ModelState.IsValid)
{
bool safetyCheck = _chartererService.GetCharterers().Any(x => x.ChartererName.Contains(model.ChartererName));
if (safetyCheck == true)
{
//Return something to the ajax failure
}
else
{
try
{
_chartererService.InsertCharterer(model);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
return PartialView("_Window_Charterer");
}
然后,该函数应捕获故障并对其进行处理,例如,我将为用户显示警报。
function form_data_fail(result) {
//capture the error from the controller and do something with it.
}
谁能解释一下这个特定设置是如何完成的?