0

I want to show data that returned from controller, in onComplete function:

function onComplete() {
    alert(); //I want to show data that returned from controller in this.
}

here is ajax link:

@Ajax.ActionLink("Upvote", "Upvote", "Author",
new AjaxOptions
{
    InsertionMode = InsertionMode.Replace,
    UpdateTargetId = "vote_count",
    OnComplete = "onComplete",
})

And my controller:

public ActionResult Upvote(Guid QuestionID)
{
     return Content("Message");
}

Thanks.

1 Answer 1

1
function onComplete(result) {
    alert(result.responseText);
}

Or if you use the OnSuccess callback:

@Ajax.ActionLink("Upvote", "Upvote", "Author",
new AjaxOptions
{
    InsertionMode = InsertionMode.Replace,
    UpdateTargetId = "vote_count",
    OnSuccess = "onSuccess",
})

you could:

function onSuccess(result) {
    alert(result);
}

For example if your controller action returned a JSON result:

return Json(new { foo = "bar" }, JsonRequestBehavior.AllowGet);

in your Success callback you already get a javascript parsed object:

function onSuccess(result) {
    alert(result.foo);
}

Notice that this is not the case with the OnComplete callback where you always get the XHR object as parameter and it is up to you to do the parsing and so on. Also notice that the OnComlpete callback is always executed, even if the AJAX call fails which obviously is not the case with the OnSuccess callback.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.