2
比方说,我发布形式的MVC控制器和动作像这样JQuery的AJAX成功与多个MVC控制器方法
function ajaxFunction() {
$.ajax({
type: "POST",
url: "ControllerName/FirstMethod",
data: $('#form').serialize(),
success: function() {
//I'm wondering if this gets run after the FirstMethod or SecondMethod
}
});
)
控制器动作做一些事情,然后重定向到这样
[HttpPost]
public ActionResult FirstMethod()
{
//Some code run here
//Send to the next method
return RedirectToAction("SecondMethod");
}
public void SecondMethod()
{
//Something else done here
}
下一个方法
所以整个过程是发布到FirstMethod,然后运行SecondMethod。我的问题是 - 何时运行Ajax success()方法?是在FirstMethod还是SecondMethod之后?
这非常有趣。感谢您的回应。直接调用public void方法会发生什么?我有一些在我的代码中,我使用ajax成功函数。这是一个不同的场景? – madvora
当重定向到返回void的action方法时,MVC实际返回一个EmptyResult,它是一个200状态代码,所以它确实会运行success()回调。否则,它取决于ActionResult类型。除了HttpNotFoundResult,HttpUnauthorizedResult,HttpStatusCodeResult之外,大多数返回2XX代码。 – br4d