1
如果我有對象myApi
與execute
功能jQuery的AJAX聽衆
var api = new myApi();
api.execute();
裏面execute
我有(* that
是myApi
實例)
function execute() {
$.ajax({
type: this.getRequestMethod(),
data: this.getDataParams(),
complete: function(xmlHttp){
that.setResult(jQuery.parseJSON(xmlHttp.responseText));
that.setHttpStatus(xmlHttp.status);
},
url: this.getUrl(),
beforeSend: setHeader
});
}
我怎樣才能使回調/監聽器,所以我可以做此
var api = new myApi();
api.execute();
var result = api.getResult();
var statusCode = api.getStatusCode();
switch(statusCode) {...};
如果我以這種方式離開它,這些底部兩行在ajax
調用完成之前執行(complete
尚未調用),因此我有undefined
變量。