提问人:Yanayaya 提问时间:4/22/2021 更新时间:4/22/2021 访问量:775
如何在 ASP.NET Core 中表单的 data-ajax-failure 函数中捕获 Ajax 错误?
How do I capture an Ajax error in the data-ajax-failure function of my form in ASP.NET Core?
问:
我想在使用不显眼的ajax的表单部分捕获我的错误消息。如果表单是通过调用的函数发布的,那么很容易做到这一点,但我没有这样做,我将成功和失败的函数调用都放在表单本身中:data-ajax-failure
$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.
}
谁能解释一下这个特定的设置是如何完成的?
答: 暂无答案
评论
if (safetyCheck == true){ //Return something to the ajax failure ...
如果要触发ajax failure回调函数,可以手动抛出错误/异常。throw new Exception("error from Process_Form action method");