这里有一个解决方法。下面的代码是一个 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
{
public class Cdc : BaseCommand
{
[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;
}
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,
fail,
"Cdc",
"Call",
path
);
};
function onDeviceReady(e) {
cdc(
{
path: "https://dev59.com/AmDVa4cB1Zd3GeqPdnQk"
},
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>
Cdc.cs
并将第一个代码块放入其中。然后我在我的index.html中包含了phonegap.js并添加了第二个代码块。启动应用程序,它完美地工作了!非常感谢。没有你我做不到! - PiTheNumber