Appcelerator:变量范围问题

发布于 2024-11-02 01:55:30 字数 1000 浏览 6 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

囍笑 2024-11-09 01:55:30

目前尚不完全清楚您的意思,但我认为您希望“connect_remote()”函数返回一些值给您。您无法在像您这样的异步环境中执行此操作。相反,您可以将一个函数传递给“connect_remote()”,该函数可以在“onload”处理程序运行时传递“data”值。

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();
     }
}

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();
     }
}
一场信仰旅途 2024-11-09 01:55:30

尝试“data = c.responseData”而不是“data = this.responseData”。只是基于这个的疯狂猜测...

Try "data = c.responseData" instead of "data = this.responseData". Just a wild guess based on this...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文