我有一个动作,它将表单发送到Ajax,如下所示:
@using (Ajax.BeginForm(MVC.Admin.DeviceGroup.Create(), new AjaxOptions { HttpMethod = "POST", OnSuccess = "saveAjaxForm", OnFailure = "SaveFailure" }))
{...}
行动是:
public virtual JsonResult Create(AddDeviceGroupViewModel deviceGroupViewModel)
{
if (ModelState.IsNotValid())
{
Response.StatusCode = (int)HttpStatusCode.BadRequest;
return Json(new { success = false, message = ModelState.FirstErrorMessage(), notificationType = NotificationType.Error }, JsonRequestBehavior.AllowGet);
}}
和我的js
功能是:
function SaveFailure(data) {
console.log('saveFailure');
$("button[type=submit]").prop('disabled', false);
var result = $.parseJSON(data.responseText);
showMessage(result.message, result.notificationType);
}
我收到了以下错误消息:
SyntaxError:JSON.parse:JSON数据第1行第1列的意外字符
IsNotValid
是ExtensionMethod:
public static bool IsNotValid(this ModelStateDictionary modelState)
{
return !modelState.IsValid;
}
和FirstErrorMessage
是:
public static string FirstErrorMessage(this ModelStateDictionary modelState)
{
return modelState.Select(row => row.Value.Errors).Where(row => row.Count > 0).FirstOrDefault().First().ErrorMessage;
}
答案 0 :(得分:1)
一小时后,我发现了问题。我在web.config
<httpErrors errorMode="Custom">
<remove statusCode="404"/>
<error statusCode="404" path="/Error/NotFound" responseMode="ExecuteURL"/>
</httpErrors>
我对它进行了评论并且工作正常。
另一种方式: 使用此代码:
Response.TrySkipIisCustomErrors = true;
或将errorMode
更改为DetailedLocalOnly
赞:
<httpErrors errorMode="DetailedLocalOnly">
<remove statusCode="404"/>
<error statusCode="404" path="/Error/NotFound" responseMode="ExecuteURL"/>
</httpErrors>