I am using the following code in Titanium Appcelerator to connect with remote Host:
var connect_remote = function(url)
{
/*
* make sure that the Device is connected before initiate process as we don't want to force
* the user to open remote stream just for sake of new entries
*/
//alert("In Func" + is_connected());
var d_data = null;
if(is_connected())
{
var c = Titanium.Network.createHTTPClient();
var data = null;
c.setTimeout(10000);
c.onload = function()
{
if (c.status == 200 )
{
data = this.responseData;
Titanium.App.Properties.setString('returnData',data);
}
};
c.error = function(e)
{
alert("Error = "+e.error);
}
c.open('GET',url);
c.send();
}
}
I want to return the value of data variable which is supposed to keep the value of the response, so that I could use but it is always returning null or undefined. How do I return the value data from it?
It's not entirely clear what you mean, but I think you want your "connect_remote()" function to return some value to you. You can't do that in an asynchronous environment like yours. Instead, you can pass a function in to "connect_remote()" that can be passed the "data" value when the "onload" handler runs.
var connect_remote = function(url, handler)
{
/*
* make sure that the Device is connected before initiate process as we don't want to force
* the user to open remote stream just for sake of new entries
*/
//alert("In Func" + is_connected());
var d_data = null;
if(is_connected())
{
var c = Titanium.Network.createHTTPClient();
var data = null;
c.setTimeout(10000);
c.onload = function()
{
if (c.status == 200 )
{
data = this.responseData;
Titanium.App.Properties.setString('returnData',data);
handler(data);
}
};
c.error = function(e)
{
alert("Error = "+e.error);
}
c.open('GET',url);
c.send();
}
}