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