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
变量。