我现在正在jQuery中进行同步AJAX调用。是否有任何方法让父函数在保持异步调用的同时返回Ajax响应。我能找到的唯一解决方案是使用回拨功能。我不想写一个回调函数,因为我打算把这个对象放在一个变量中。jQuery延迟Ajax调用和返回Ajax响应
例如
var ajax = serviceinterface.rawServiceCall(“http://user.myservice.com/endpoint”, “post”, {name : “xyz”});
我可能还需要使用这样的
$.when(serviceinterface.rawServiceCall(endpoint1, “get”), serviceinterface.rawSCACall(endpoint2, ‘get’)).done(function(){
// do something here
});
这里是我的功能码,同步调用工作绝对没问题,但我想打一个异步调用。
rawServiceCall : function(endpoint, type, payload) {
type = typeof type !== 'undefined' ? type : "get";
payload = typeof type !== 'undefined' ? payload : null;
return $.ajax({
url : endpoint,
type : type,
data : payload,
async : false,
dataType : 'json',
headers: {
accept : "application/json"
},
success : function(data){
return data;
}
});
}
P.S-我不想浪费CPU周期或保持网页的其他部分等待这首先加载。或者有什么更好的方法呢?