这是我在ASP.NET MVC Razor视图中创建的jQuery方法.所需的功能是:
>选择textAreas
>触发一系列ajax请求
>完成所有ajax请求后,显示警告对话框
代码似乎正常工作,除了警报“ok”对话框多次显示,每次请求一次.这表明正在为每个请求调用.then()方法,而不是等待所有请求完成.我在这做错了什么?
// Save changed entity notes
function saveChangedNotes() {
var ajaxReqs = [];
$('textarea').each(function() {
ajaxReqs.push($.ajax({
type: "POST",url: "@Url.Action("UpdateCompanyNote","Company",new {companyId = Html.CompanyIdFromRouteValues()})",data: {
noteId: this.id,noteText: $(this).val()
}
})
);
// When all ajax complete..
$.when.apply($,ajaxReqs).then(function() {
alert('ok');
}).fail(function() {
alert('error');
});
});
}