MVC Ajax Javascript OnComplete参数函数'undefined'

时间:2014-07-26 10:18:43

标签: javascript ajax asp.net-mvc

我遇到一个问题,即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()函数。

感谢您的帮助!

1 个答案:

答案 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  
// ...
}