Skip to content

Instantly share code, notes, and snippets.

@pec1985
Created August 1, 2011 20:41
Show Gist options
  • Save pec1985/1118941 to your computer and use it in GitHub Desktop.
Save pec1985/1118941 to your computer and use it in GitHub Desktop.
XHR function with callback
HTTPRequest({
ur:'http://example.com/somefile.json',
type:'GET',
success: function(a){
var json = JSON.parse(a);
// do something else
},
error: function(response, message){
// handle error
}
});
function HTTPRequest(params){
if(!params || !params.url){
alert('No params passed!!');
return;
}
if(!params.success){
alert('Give the XHR call a success callback!!');
return;
}
if(!params.error){
alert('Give the XHR call an error callback!!');
return false;
}
if(!params.type){ params.type = 'GET'; }
var xhr = Ti.Network.createHTTPClient();
xhr.onload = function(){
params.success(this.responseText);
};
xhr.onerror = function(e){
params.error(this.responseText, e);
// alert('Server Error!');
};
xhr.open(params.type,params.url);
xhr.send();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment