我使用jQuery.Deferred并注册done,fail和then处理程序:
$.when( some_ajax(url) )
.done(function(result){})
.fail(function(){})
.then(function(){}); //just like that, with a single parameter
Run Code Online (Sandbox Code Playgroud)
我发现当我的ajax调用成功,done并按then顺序调用回调函数时.但是当ajax失败时,会fail调用回调,但是我没有进行then回调.
我已经阅读了jQuery.Deferred文档,但无法找到有关此行为原因的提示.
当使用always替代的then,它被称为在这两种情况下-成功和失败(第一done/ fail被调用,然后always叫).该文档似乎并未表明我所描述的场景之间always和之间的预期差异then,为什么它们的行为有所不同?
一个例子,以明确我想做什么.这就是我通常会做的事情:
function success(data, status, jqxhr){
if ( data.error )
return failure(jqxhr, status, data.error);
// process data
}
function failure(jqxhr, status, err){
...
}
$.ajax( ... )
.done(success)
.fail(failure)
Run Code Online (Sandbox Code Playgroud)
有什么办法,我可以用匿名函数来完成这个,就像这样吗?
$.ajax( ... )
.done(function(data, status, jqxhr){
if(data.error)
// what do i need to do here to jump in to the fail handler?
})
.fail(function(jqxhr, status, err){
...
})
Run Code Online (Sandbox Code Playgroud) 像这样读JSON-Service:
$.ajax({
url:'activeIDs',
success : function(data){ // data = [14,15]
var tableRows = [];
for (var dataIndex=0; dataIndex < data.length; dataIndex++) {
var isLast = dataIndex == (data.length - 1);
$.ajax({
url: 'info?id=' + data[dataIndex],
success: function(data2) { // "foo", "bar"
tableRows.push(data2.name);
if (isLast) {
alert(tableRows.length);
}
}
});
}
}
});
Run Code Online (Sandbox Code Playgroud)
第一个网络跟踪是:
[14,15]"foo""bar"在这种情况下,警报给出"2".
秒网络跟踪是不同的:
[14,15];"foo"(现在需要很长时间)"bar"在这种情况下警报提供 …
有关如何使用JQuery的延迟方法以及检测所有已更改的表单并将每个表单作为Ajax帖子提交的功能的任何想法?
如果我只列出一大堆表单提交,但是如果我使用...我可以得到相同的工作
$('form.changed').each(function(){
return $(this).submitWithAjax();
});
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用的更完整版本的代码就在这里...... 在JS Fiddle
提前致谢!
所以在使用jQuery延迟并且$.when并行加载许多对象.
$.when(
a.ajax(), b.ajax(), c.ajax()
).then(
//do something when all are complete
complete();
);
Run Code Online (Sandbox Code Playgroud)
现在,b.ajax()有时会失败,但我真的不在乎.我只想等到所有调用完成后再调用complete().
不幸的是,一旦b失败,when()拒绝,并且永远不会触发then()回调.这是AFAIK的预期行为$.when(),但在这种情况下非常适合我.
我实际上想要一种说法:
$.when(
a.ajax(), b.ajax().fail(return success), c.ajax()
).then(...)
Run Code Online (Sandbox Code Playgroud)
或者可能有不同的使用方式when(),或更合适的构造?
我仍然试图使用JQuery的Deferred对象来解决问题,并且正在抓住一个特定的问题.在下面的代码中,我最初尝试链接deferred.then()但它从未起作用.所有三个功能一次执行.只有在我的同事向我指出这个pipe功能后,事情就会落到实处.问题是,为什么pipe()工作,但不是then()?
var otherDefer = function(msg){return function(){return testDefer(msg)}};
var there = otherDefer("there,");
var guy = otherDefer("guy.");
function testDefer(msg) {
var deferred = $.Deferred();
pretendAjaxCall( function() {
$('<li>'+msg+'</li>').appendTo('#msgOut');
deferred.resolve();
});
return deferred.promise();
}
function pretendAjaxCall(callback) {
setTimeout(callback,1500);
}
$.when(testDefer("Hi")).pipe(there).then(guy);?
Run Code Online (Sandbox Code Playgroud)
我也试过return deferred而不是return deferred.promise()在使用时when().then().then().
以上代码的jsFiddle:http://jsfiddle.net/eterpstra/yGu2d/
我有一个带回调的ajax调用.我想在回调结束后调用另一个方法.我使用了来自jQuery的promise API,但正如你在下面看到的那样,在第一个方法完成之前调用第二个方法.
有任何想法吗?
my.data = function () {
var loadFlights = function (callback) {
//$.getJSON("/api/Acceptance/", function (data) {
// callback(data);
//});
$.getJSON("/api/Acceptance").success(function (data) {
console.log("first: " + new Date().getTime());
callback(data);
})
.then(console.log("second:" + new Date().getTime()));
};
return { load: loadFlights }
}();
Run Code Online (Sandbox Code Playgroud)
结果到控制台:
second:1357393615115
first: 1357393615246
Run Code Online (Sandbox Code Playgroud) 我正在从这样的函数返回一个promise:
resultPromise = dgps.utils.save(opportunity, '/api/Opportunity/Save', opportunity.dirtyFlag).then(function () {
self.checklist.saveChecklist(opportunity).then(function () {
self.competitor.save(opportunity.selectedCompetitor()).then(function ... etc.
return resultPromise;
Run Code Online (Sandbox Code Playgroud)
假设上面的函数叫做save.
在我想做的调用函数中,等待整个链完成,然后做一些事情.我的代码看起来像这样:
var savePromise = self.save();
savePromise.then(function() {
console.log('aftersave');
});
Run Code Online (Sandbox Code Playgroud)
结果是,当承诺链仍在运行时,'aftersave'被发送到控制台.
整条链完成后我该怎么做?
我正在尝试理解whenjQuery中的函数和延迟对象.
$.when($.getJSON('/echo/json', function () {
console.log('sucess');
}, function () {
console.log('error');
})).then(console.log('get JSON ready!'));
Run Code Online (Sandbox Code Playgroud)
此示例返回:
get JSON ready!
sucess
Run Code Online (Sandbox Code Playgroud)
...但我希望首先实现成功回调:
sucess
get JSON ready!
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
我想在不使用jQuery的情况下实现基本的Deferred对象.在这里,我将仅实现已完成和失败的回调,具有解析和拒绝功能.以及promise与此功能相关的课程关联方法.
我在纯js(编辑)中执行以下实现:
function Deferred() {
var d = {};
d.resolve = function() {
d.done(arguments);
}
d.reject = function() {
d.fail(arguments);
}
d.promise = function() {
var x = {};
x.done = function(args) {
return args;
}
x.fail = function(args) {
return args;
}
return x;
}
return d;
}
var v;
var setVal = function() {
var d = new Deferred();
setTimeout(function() {
v = 'a value';
d.resolve(this);
}, 5000);
return d.promise();
};
setVal().done(function() {
console.log('all done :' …Run Code Online (Sandbox Code Playgroud)