Jquery 如何使AJAX同步
我知道Ajax的本质是异步的,因此我的代码是错误的。 但我需要帮助找到另一个解决方案 案例是:我使用以下方法将数据插入数据库:Jquery 如何使AJAX同步,jquery,ajax,Jquery,Ajax,我知道Ajax的本质是异步的,因此我的代码是错误的。 但我需要帮助找到另一个解决方案 案例是:我使用以下方法将数据插入数据库: self.insertTextItem = function (item) { console.log("Insert TextItem with Service"); return $.ajax({ type: "POST", url: serviceRoot + "InsertTextbatchTagItem
self.insertTextItem = function (item) {
console.log("Insert TextItem with Service");
return $.ajax({
type: "POST",
url: serviceRoot + "InsertTextbatchTagItem",
beforeSend: serviceFramework.setModuleHeaders,
data: item,
cache: false
});
};
item.Id = null; // Id is autocreated in db.
item.Text = "Finally Friday!";
self.insertTextItem(item)
.done(function (newtext) {
item.textbatchId = newtext.Id;
}
//continuous code…
这项工作在大多数情况下都可以找到。
例外情况是当我需要立即从数据库返回数据时。
但是,我需要“连续代码”来等待Id返回。
显而易见的解决方案是将“连续代码”放入回调函数中,但实际上,我在函数中调用self.insertTextItem,因此调用函数将不等待返回。(显然,回调函数在调用函数中结束。)
在这种情况下,如何重写self.insertTextItem函数
例外情况是当我需要立即从数据库返回数据时。但是,我需要“连续代码”来等待Id返回
最好不要让这种情况发生,而是接受基于浏览器和网络编程的事件驱动、异步特性
较小的选项是强制ajax请求同步。目前,在jQuery1.x中,可以通过向调用中添加async:false
来实现这一点;您还必须开始使用自己的$。延迟而不是从ajax
获得的延迟,因为从jquery1.8开始,使用async:false
和jqXHR
的内置承诺是不推荐的。因此:
self.insertTextItem = function (item, synchronous) { // <== Optional flag
var deferred = $.Deferred(); // <== Your own Deferred
console.log("Insert TextItem with Service");
$.ajax({
type: "POST",
url: serviceRoot + "InsertTextbatchTagItem",
beforeSend: serviceFramework.setModuleHeaders,
data: item,
cache: false,
async: synchronous === true ? false : true, // <=== Use it, ignoring `undefined` and othe rfalsey values
success: function(data) { // Handling you Deferred
deferred.resolveWith(data); // (this is probably incomplete)
}, //
error: function() { //
deferred.reject(); //
} //
});
return deferred.promise(); // Return the promise for your Deferred
};
self.insertTextItem=函数(项,同步){//
例外情况是当我需要立即从数据库返回数据时。但是我需要“连续代码”来等待Id返回
最好不要让这种情况发生,而是接受基于浏览器和网络编程的事件驱动、异步特性
较小的选项是强制ajax请求同步。目前,在jQuery1.x中,您可以通过在调用中添加async:false
来实现这一点;您还必须开始使用自己的$。延迟的而不是从ajax
获得的请求,因为从jQuery1.8开始,使用async:false
和jqXHR
的内置承诺已被弃用。因此:
self.insertTextItem = function (item, synchronous) { // <== Optional flag
var deferred = $.Deferred(); // <== Your own Deferred
console.log("Insert TextItem with Service");
$.ajax({
type: "POST",
url: serviceRoot + "InsertTextbatchTagItem",
beforeSend: serviceFramework.setModuleHeaders,
data: item,
cache: false,
async: synchronous === true ? false : true, // <=== Use it, ignoring `undefined` and othe rfalsey values
success: function(data) { // Handling you Deferred
deferred.resolveWith(data); // (this is probably incomplete)
}, //
error: function() { //
deferred.reject(); //
} //
});
return deferred.promise(); // Return the promise for your Deferred
};
self.insertTextItem=function(item,synchronous){/您可以将$.ajax的异步属性设置为false。默认情况下为true
$.ajax({
type: "POST",
async : false,
url: serviceRoot + "InsertTextbatchTagItem",
beforeSend: serviceFramework.setModuleHeaders,
data: item,
cache: false
});
您可以将$.ajax的异步属性设置为false。默认情况下为true
$.ajax({
type: "POST",
async : false,
url: serviceRoot + "InsertTextbatchTagItem",
beforeSend: serviceFramework.setModuleHeaders,
data: item,
cache: false
});
在done()中实现连续代码
callback.@Asle使ajax同步可以挂起应用程序。如果您希望在ajax调用完成时数据可用,请尝试使用回调函数。通常的解决方案是禁用ui,可能显示一个带有覆盖的ajax微调器,然后在ajax回调中重新启用。同步ajax会锁定ui并导致不愉快的结果体验我正在使用回调函数,但“连续代码”不会等待从视图模型调用“insertTextItem”函数。因此,我需要用同步函数替换AJAX。因此,重构“连续代码”并从AJAX回调调用它在done()中实现连续代码
callback.@Asle使ajax同步可以挂起应用程序。如果您希望在ajax调用完成时数据可用,请尝试使用回调函数。通常的解决方案是禁用ui,可能显示一个带有覆盖的ajax微调器,然后在ajax回调中重新启用。同步ajax会锁定ui并导致不愉快的结果experienceI正在使用回调函数,但“连续代码”不会作为“insertTextItem”等待-函数是从视图模型调用的。因此,我需要用同步函数替换AJAX。因此,重构“连续代码”,并从AJAX调用调用它back@op我劝你不要吃这个approach@andrew,我同意你的看法,回调应该是这种情况下的理想选择。@op我劝你不要这样做approach@andrew,我同意你的看法,回调应该是这种情况下的理想选择。