我想在onComplete函数中显示从控制器返回的数据:
function onComplete() {
alert(); //I want to show data that returned from controller in this.
}
这里是ajax链接:
@Ajax.ActionLink("Upvote", "Upvote", "Author",
new AjaxOptions
{
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "vote_count",
OnComplete = "onComplete",
})
我的控制员:
public ActionResult Upvote(Guid QuestionID)
{
return Content("Message");
}
感谢。
答案 0 :(得分:1)
function onComplete(result) {
alert(result.responseText);
}
或者如果您使用OnSuccess
回调:
@Ajax.ActionLink("Upvote", "Upvote", "Author",
new AjaxOptions
{
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "vote_count",
OnSuccess = "onSuccess",
})
你可以:
function onSuccess(result) {
alert(result);
}
例如,如果您的控制器操作返回了JSON结果:
return Json(new { foo = "bar" }, JsonRequestBehavior.AllowGet);
在您的Success回调中,您已经获得了一个javascript解析对象:
function onSuccess(result) {
alert(result.foo);
}
请注意,OnComplete
回调不是这种情况,您总是将XHR对象作为参数获取,并由您来解析等等。还要注意OnComlpete回调总是被执行,即使AJAX调用失败,这显然不是OnSuccess回调的情况。