加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

windows-phone-7 – Windows phone中phonegap中缺少白名单的解决

发布时间:2020-12-14 02:55:57 所属栏目:Windows 来源:网络整理
导读:在我的 other question中,我发现Windows手机没有白名单. 现在我正在寻找本机代码解决方法,但我从未为Windows手机编写过一行本机代码.所以这对我来说并不容易.我想我可以下载这样的页面: void GetAirportData(){ var url = new Uri("http://server.example.c
在我的 other question中,我发现Windows手机没有白名单.

现在我正在寻找本机代码解决方法,但我从未为Windows手机编写过一行本机代码.所以这对我来说并不容易.我想我可以下载这样的页面:

void GetAirportData()
{
  var url = new Uri("http://server.example.com/data.php",UriKind.Absolute);
  var webClient = new WebClient();
  webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
  webClient.OpenReadAsync(url,url);
}

但是如何将这些数据传递到我的javascript应用程序?

解决方法

这是一个解决方法.以下代码是实现跨域调用功能的Phonegap命令.

using System;
using System.IO;
using System.Net;
using System.Runtime.Serialization;
using WP7CordovaClassLib.Cordova;
using WP7CordovaClassLib.Cordova.Commands;
using WP7CordovaClassLib.Cordova.JSON;

namespace Cordova.Extension.Commands //namespace is predefined,don't change it!
{
    public class Cdc : BaseCommand //Cross domain call
    {
        [DataContract]
        public class CdcOptions
        {
            [DataMember(Name = "path")]
            public string Path { get; set; }
        }

        public void Call(string args)
        {
            CdcOptions options = JsonHelper.Deserialize<CdcOptions>(args);

            var url = new Uri(options.Path,UriKind.Absolute);

            var webClient = new WebClient();

            webClient.OpenReadCompleted += (s,e) =>
            {
                if (e.Error != null)
                {
                    DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR,"Error"));
                    return;
                }

                //Stream -> string
                var sr = new StreamReader(e.Result);
                var result = sr.ReadToEnd();

                DispatchCommandResult(
                    new PluginResult(PluginResult.Status.OK,result));
            };

            webClient.OpenReadAsync(url,url);

        }
    }
}

在客户端测试:

????
??????
????????
????????
???????????

<script type="text/javascript">

          function cdc(path,success,fail) {

              PhoneGap.exec(
                            success,//success
                            fail,//fail
                            "Cdc",//service
                            "Call",//action
                             path //args
                           );
          };

          function onDeviceReady(e) {

                cdc(
                    {
                        path: "https://stackoverflow.com/questions/9291809/workaround-for-missing-whitelist-in-phonegap-for-windows-phone"
                    },function (arg) {
                        document.getElementById('test').innerHTML = arg;
                    },function (arg) {
                        document.getElementById('test').innerHTML = arg;
                    });

            }

            document.addEventListener("deviceready",onDeviceReady,false);


      </script>
  </head>
    <body>
        <div id="test"></div>
    </body>
</html>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读