我有以下代碼。javascript模式下的Ajax回調
var foo = {
ajaxcall : function(){
var _obj = {};
$.ajax({
headers: {
'Content-Type': "application/json; charset=utf-8",
'dataType': "json"
},
async: false,
url: "/getservertime"
}).done(function(resp,stat) {
resp = JSON.parse(resp);
_obj.resp = resp;
console.log(_obj);
return _obj;
});
},
init: function(){
this.somefunction(this.ajaxcall());
},
somefunction: function(_data){
console.log(_data); // coming as undefined
}
}
foo.init();
我如何可以等待AJAX的執行完成,然後再執行somefunction
方法。我已經試過async: false
。看起來像一些其他問題。