使用Ajax调用对公共MVC方法的调用。
Dart.LoadProfile.DetailProfileLoad = function (url) {
$.ajax({
url: url,
type: 'POST',
data: null,
xhrFields: {
withCredentials: true,
},
success: function (data) {
//get JSON data from response
jQuery.each(data, function (index, itemData) {
//Code here to display result
});
},
error: function (jqXHR, ajaxSettings, thrownError) {
console.log(thrownError);
},
});
};
非异步
[HttpPost]
[AllowAnonymous]
public ActionResult GeneralLoadProfileResult(long fid, long uniqueId, string loadyear)
{
try
{
//Code here. Average 1 - 1.5 min to compelte calculation
return Json(new { result = txLoadReport });
}
catch (Exception)
{
throw;
}
}
异步
[HttpPost]
[AllowAnonymous]
public async Task<ActionResult> GeneralLoadProfileResult(long fid, long uniqueId, string loadyear)
{
try
{
return await Task.Run<ActionResult>(() =>
{
//Code here. Average 1 - 1.5 min to compelte calculation
return Json(new { result = txLoadReport });
});
}
catch (Exception)
{
throw;
}
}
有人可以解释一下上面提供的样本中使用Async(如果有的话)的好处吗?进行异步与非异步调用会更有效率,即当多个用户同时发出相同的请求时?
谢谢。