Skip to content

Instantly share code, notes, and snippets.

@automotua
Created February 2, 2016 02:06
Show Gist options
  • Select an option

  • Save automotua/b5c170fea534e609df8e to your computer and use it in GitHub Desktop.

Select an option

Save automotua/b5c170fea534e609df8e to your computer and use it in GitHub Desktop.
Generic Ajax API Call Interface with jQuery
/*
following is a better implementation of ajax call
It is better to handle 401, 202...cases
It is better for utoken management.
*/
function callAPI(url, arg, type, data, auth, query){
if(url){
var defer = {
success_callback_fn: function(e){
},
error_callback_fn: function(e){
},
done: function(callback){
if(typeof callback == 'function'){
this.success_callback_fn = callback;
}
//this.resolve() function has a timeout mechanism,
//so it is safe to call it here while add fail callback function at next step
this.resolve();
return this;
},
fail: function(callback){
if(typeof callback == 'function'){
this.error_callback_fn = callback;
}
this.resolve();
return this;
},
construct: function(url, arg, type, data, auth, query){
this.url = url;
this.argument = arg;
this.type = type;
this.data = data;
this.auth = auth;
this.query = query;
},
resolve: function(){
if(!this.resolved){
this.resolved = true;
setTimeout(function(){
//NOTED:
//should not use "this" as argument,
//because in timeout function context, "this" is not the defer!
ajaxService.resolveDefer(defer);
}, 0);
}
},
url:undefined,
argument: undefined,
type: undefined,
data: undefined,
auth: undefined,
query: undefined,
resolved: false
};
defer.construct(url, arg, type, data, auth, query);
return defer;
}else{
return undefined;
}
};
ajaxService.resolveDefer = function(defer){
if(defer && defer.url){
var requestAtt = ajaxService.getRequestRelated(defer.url);
if(requestAtt.requireAuth && !defer.auth){
ajaxService.getAuthForDefer(defer);
}else{
var callApi = callApi_(defer.url, defer.argument, defer.type, defer.data, defer.auth, defer.query);
callApi.done(function(data, textStatus, jqXHR) {
ajaxService.ajaxLog.record_success(defer, data);
ajaxService.sendcallback(data, defer.success_callback_fn);
});
callApi.fail(function(jqXHR, textStatus, errorThrown) {
ajaxService.ajaxLog.record_fail(defer, jqXHR);
if(jqXHR.status === 401){
if(defer['count_401'] >= 3){
ajaxService.sendcallback(jqXHR, defer.error_callback_fn);
}else{
defer['count_401'] = (defer['count_401'] ? defer['count_401'] + 1 : 1);
ajaxService.getAuthForDefer(defer, 1);
}
}else if(jqXHR.status === 202){
//if receive 202, try another 3 times.
//if if still cannot work, throw error
if(defer['count_202'] >= 3){
ajaxService.sendcallback(jqXHR, defer.error_callback_fn);
}else{
defer['count_202'] = (defer['count_202'] ? defer['count_202'] + 1 : 1);
setTimeout(function(){ajaxService.resolveDefer(defer);}, 2000);
}
}else{
ajaxService.sendcallback(jqXHR, defer.error_callback_fn);
}
});
}
return 1;
}else{
mobileService.log('Defer cannot be resolved, no valid URL found!');
return 0;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment