本地主机上的 WCF 服务在浏览器中工作,但在 PhoneGap 中不起作用
这是我的 Web 服务,在 Fiddler 和我的浏览器中运行得非常好: http://localhost:11458/PlayerSvc.svc/GetPLayers
然后我尝试通过 Jquery 调用它在phonegap中失败: 这是我在phonegap 中使用Eclipse 的jquery:
<script type="text/javascript">
$.ajax({
url: 'http://localhost:11458/PlayerSvc.svc/GetPlayers',
dataType: 'json',
// timeout: 55000,
// beforeSend: function(xhr) {
//Possible to set any required headers here
//xhr.setRequestHeader('Authorization', 'username password');
// },
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert('failed');
},
success: function(data) {
alert('success');
}
});
</script>
为了确保我的jquery 没有任何问题,我使用此代码测试了另一个Web 服务,并且成功了。我不确定如何收集有关如何调试此问题的更多信息。我尝试用 console.log(上面的 javascript) 包装我的 javascript,但我无法让它工作。正如您在上面看到的,我只是使用警报功能来输出失败的错误消息。有什么想法吗?我想我可以尝试使用 try catch,然后看看是否可以输出异常......
Here is my web service that works perfectly fine in Fiddler and in my browser:
http://localhost:11458/PlayerSvc.svc/GetPLayers
I then tried to call this via Jquery in phonegap and it failed:
here is my jquery in phonegap using Eclipse:
<script type="text/javascript">
$.ajax({
url: 'http://localhost:11458/PlayerSvc.svc/GetPlayers',
dataType: 'json',
// timeout: 55000,
// beforeSend: function(xhr) {
//Possible to set any required headers here
//xhr.setRequestHeader('Authorization', 'username password');
// },
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert('failed');
},
success: function(data) {
alert('success');
}
});
</script>
To make sure there was nothing wrong with my jquery I tested another webservice with this code and it was successful. I am not sure how to gather more information on how to debug this. I tried wrapping my javascript with console.log(javascript above) and I could not get that to work. As you can see above I just used the alert function to output the failed error message. Any ideas? I think i may try using a try catch and then seeing if I can output the exception...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否在 Android 模拟器中运行 PhoneGap 应用程序?如果是,我认为模拟器中的localhost(127.0.0.1)指的是模拟器本身而不是主机。也许您需要尝试另一个 IP 地址(可能是 10.0.2.2)。
这是解决此问题的问题/答案:
在 android 模拟器中测试 localhost
Are you running the PhoneGap application in the Android emulator? If yes, I think the localhost (127.0.0.1) in the emulator refers to the emulator itself and not the host machine. Perhaps you need to try another IP address (possibly 10.0.2.2 works).
Here's a question/answer that addresses this issue:
test the localhost in android emulator
您需要允许白名单网址并将您的主机地址添加到phonegap.plist 文件中的外部主机。
http://geekaboo.wordpress.com/ 2011/11/20/ios5-whitelist-url-unauthorized-using-phonegap/
You need to allow whitelist urls and add your host address to external hosts in your phonegap.plist file.
http://geekaboo.wordpress.com/2011/11/20/ios5-whitelist-url-unauthorized-using-phonegap/