我遇到一个问题,即javascript OnComplete函数的事件参数没有定义get_response()。
我的观点:
<div id="result"></div>
@using (Ajax.BeginForm("Login", "Security", null,
new AjaxOptions
{
UpdateTargetId = "result",
OnBegin = "OnAjaxBegin",
OnComplete = "OnAjaxComplete",
OnFailure = "OnAjaxFailure"
}))
{
...
}
</div>
控制器:
[HttpPost]
public ActionResult Login(HomeViewModel viewModel)
{
if (Request.IsAjaxRequest())
{
// return PartialView();
return Json(new { redirectTo = "/Admin" });
}
return View();
}
javascript:
function OnAjaxComplete(sender) {
var response = sender.get_response(); // <-- get_response is 'undefined'
// ...
}
因此发送方参数上没有get_response()函数。
感谢您的帮助!
答案 0 :(得分:2)
只需在您的ajaxrequest中添加HttpMethod = "Post"
,如果您想将某些数据发送到OnAjaxComplete
函数,请在OnSuccess
上执行以下操作: -
new AjaxOptions
{
UpdateTargetId = "result",
OnBegin = "OnAjaxBegin",
OnSuccess = "OnAjaxComplete(data)",
OnFailure = "OnAjaxFailure",
HttpMethod = "Post",
}
function OnAjaxComplete(data) {
var response = data.redirectTo; <--- //redirectTo which is returned from JSON
// ...
}