【发布时间】:2018-09-15 05:26:51
【问题描述】:
我正在尝试找到一种解决方案来从 ajax 响应返回数据,而无法执行给定的回调函数。
以下代码已修复(第三方api脚本),我唯一能控制的是myfunction的内容:
var myfunction( param ) {
$.get( '/search', { q: param } );
// no further ideas :D
return; // <-- idealy the response of the ajax call
};
Promise.resolve()
.then(
function() {
return myfunction( param );
}
).then(
function( res ) {
return somefunction( res );
}
).catch(
function( res ) {
return errorfunction( res );
}
);
myfunction 的内容现在是一个执行数据库搜索的 ajax 调用。
由于我不能使用回调函数,所以我被困在那里。我真的不想将 $.ajax 的 async 选项设置为 false。
有什么想法吗?
【问题讨论】:
-
你检查了吗? stackoverflow.com/questions/5316697/…(第二个答案,多次更新)
-
谢谢@JaxTeller ;-) 我通过只返回 ajax 调用得到了解决方案。看我的回答。
标签: javascript jquery ajax callback promise