【发布时间】:2014-01-07 05:35:57
【问题描述】:
这是我在 ASP.NET MVC Razor 视图中创建的 jQuery 方法。所需的功能是:
- 选择文本区域
- 触发一组 ajax 请求
- 所有 ajax 请求完成后,显示警报对话框
代码似乎工作正常,除了多次显示警告“确定”对话框,每个请求一次。这表明正在为每个请求调用 .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');
});
});
}
【问题讨论】: