JQuery AJAX 多个 MVC 控制器方法成功
JQuery AJAX Success With Multiple MVC Controller Methods
假设我 post 一个 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
}
所以整个过程就是post到FirstMethod,然后运行SecondMethod。我的问题是 - Ajax success() 方法何时 运行?是在 FirstMethod 还是 SecondMethod 之后?
RedirectToAction returns HTTP 状态代码 302,这使得 AJAX 对重定向执行 GET URL(SecondMethod)。
jQuery AJAX 只有在返回 2XX HTTP 代码时才会调用成功。如果 SecondMethod returns 状态代码为 2XX 的东西(例如 View),它将是。否则永远不会被调用。
假设我 post 一个 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
}
所以整个过程就是post到FirstMethod,然后运行SecondMethod。我的问题是 - Ajax success() 方法何时 运行?是在 FirstMethod 还是 SecondMethod 之后?
RedirectToAction returns HTTP 状态代码 302,这使得 AJAX 对重定向执行 GET URL(SecondMethod)。
jQuery AJAX 只有在返回 2XX HTTP 代码时才会调用成功。如果 SecondMethod returns 状态代码为 2XX 的东西(例如 View),它将是。否则永远不会被调用。