Skip to content

@codeboxed /call.js
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Send requests to a server using Titanium API
/**
* Filename: Call.js
* Author: Codeboxed
* URL: http://www.codeboxed.com
* Date: March 26, 2011
* Platform: Titanium
*/
/**
* Call
* @param {object} options
*/
var Call = function(options) {
//Private
var serverURL = options.url,
method = options.method,
timeout = options.timeout;
/**
* Private method used to send the request to the server
* @param {object} params
* @returns string|boolean|null
*/
var callAction = function(params) {
var httpClient = Titanium.Network.createHTTPClient(),
result = null;
httpClient.setTimeout(timeout);
httpClient.onerror = function(e) {
Titanium.API.info('HTTP error: ' + e);
};
if (httpClient.open(method, serverURL, false)) {
httpClient.receive(function(response) {
result = response.toString();
}, params);
} else {
Titanium.API.info('cannot open connection');
result = false;
}
return result;
};
//Public
return {
call: function(params) {
return callAction(params);
}
};
};
// HOW TO USE
var initOptions = {
url: 'http://www.example.com',
method: 'POST',
timeout: 2000
};
var callObject = new Call(initOptions);
var callParams = {
var1: 'var1',
var2: 'var2'
};
var response = callObject.call(callParams);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.