Appcelerator:变量范围问题
我在 Titanium Appcelerator 中使用以下代码来连接远程主机:
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();
}
}
我想返回 data 变量的值,该变量应该保留响应的值,以便我可以使用,但它是总是返回 null 或未定义。如何从中返回值数据?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
目前尚不完全清楚您的意思,但我认为您希望“connect_remote()”函数返回一些值给您。您无法在像您这样的异步环境中执行此操作。相反,您可以将一个函数传递给“connect_remote()”,该函数可以在“onload”处理程序运行时传递“data”值。
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.
尝试“data = c.responseData”而不是“data = this.responseData”。只是基于这个的疯狂猜测...
Try "data = c.responseData" instead of "data = this.responseData". Just a wild guess based on this...