Skip to content

Instantly share code, notes, and snippets.

@vinothbabu
Last active October 10, 2015 01:27
Show Gist options
  • Save vinothbabu/3610365 to your computer and use it in GitHub Desktop.
Save vinothbabu/3610365 to your computer and use it in GitHub Desktop.
Generic HTTPClient or Ajax Class which only creates a single instance.
var JSONCall = function(url,data, onLoad, onError){
this.url = url;
this.data = data;
this.onLoad = onLoad;
this.onError = onError;
};
JSONCall.prototype = {
call: function(){
if(typeof JsonClient==='undefined'){
JsonClient = Titanium.Network.createHTTPClient();
}
JsonClient.open("POST", this.url);
//setting Request Header
JsonClient.setRequestHeader("Content-type", "application/json");
JsonClient.send(this.data);
JsonClient.onload = this.onLoad;
JsonClient.onerror = this.onError;
}
};
// create callbacks
var onLoad = function(response){ /* do something with response */ },
onError = function(error){ /* do something with error */ };
// create instance
var jsonCall = new JSONCall(url,"servicename", myLoad, myError);
// do a call
jsonCall.call();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment