2

MVC 4 Web 应用程序、JQuery

我想展示一个冗长程序的进度。

以下是从 JSON 调用的,这需要几秒钟......

基本上,我想做的是:

[HttpPost]
public JsonResult Update(var x)
{
    return Json(new { Result = "Working", Message = "Waiting on Server" });
    var y = contactAnotherServer(x);
    return Json(new { Result = "Working", Message = "Crunching Data" });
    var finished = doSomethingWithData(y);
    return Json(new { Result = "OK", Message = "Done" });
} 

显然,这将在第一个返回语句处终止。

这将如何完成?

谢谢!

4

1 回答 1

0

要像这样“自己动手”,您需要三个单独的 JsonResult 方法和一个相当复杂的 javascript 函数。IE:

控制器

public JsonResult Step1()
{
    return Json(new { Result = "Working", Message = "Waiting on Server" });
}
public JsonResult Step2()
{
    return Json(new { Result = "Working", Message = "Crunching data" });
}
public JsonResult Step3()
{
    return Json(new { Result = "Ok", Message = "Done" });
}

JS

// urls
var urls = ["/step1","/step2","/step3"];

// counter
var counter = 0;

// recursive method
var fetchAndUpdateRecursive(url){

   // get data
   $.ajax(url, {
        async: false,
        success: function(r){

              // use r to update something visually
              // then...

              // change the url
              counter++;
              url = urls[counter];

              // go again
              fetchAndUpdateRecursive(url);
        };
   });
};

// go
var urlToStartWith = urls[counter];
fetchAndUpdateRecursive(urlToStartWith);
于 2013-04-05T14:20:05.457 回答