【发布时间】:2013-07-17 01:21:31
【问题描述】:
我想将我的 ajax 函数的结果传递给另一个函数(有点像“冒泡一个事件”)。我真的需要保持 ajax 包装函数原样,即通用,所以我正在寻找一个不涉及 myAjaxHandler()
$('button').on(
'click',
myAjaxHandler()
);
function myAjaxHandler(){
return $.ajax({
...
});
}
myAjaxHandler().done(
function(result){
console.log(result); // shows 'hello'
}
).then(
console.log('hi') // shows before 'hello'
);
我先是“嗨”,然后是“你好”,但我想要的是另一种方式:先是“你好”,然后是“嗨”。我确定我没有以正确的方式使用.then(),坦率地说,我开始认为我永远不会真正掌握$.Deferreds - 我假设$.ajax 是一个Deferred 具有所有好处的对象和特质……
【问题讨论】:
-
您的
then回调不是函数。 -
+1 作为解决方案 - 太糟糕了,我无法接受。