PhoneGap不想使用REST服务,无论我做什么 [英] PhoneGap doesn't want to use REST service no matter what I do

查看:267
本文介绍了PhoneGap不想使用REST服务,无论我做什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PhoneGap应用程序,我使用AngularJS。我使用一个很简单的$ http调用我的Node后端:

I have a PhoneGap app that I am using AngularJS with. I'm using a pretty simple $http call to my Node backend:

$http.get("http://localhost:6969/random")
   .then(function(response) {
        $scope.images = response.data;
  });

无论如何,PhoneGap从不会碰到后端。我已经在一个正常的浏览器测试它,并按预期工作。

No matter what, PhoneGap never hits the backend. I have tested it in a normal browser and it works as expected.

我明显读了一堆,它大多数人修复它使用白名单,但在我的 config.xml ,我的白名单是可以开放的:

I have obviously read a bunch about it and most people fix it using whitelisting, but in my config.xml, my whitelisting is about as open as can be:

<plugin name="cordova-plugin-whitelist" source="npm" spec="1.1.0" />
  <allow-navigation href="*" subdomains="true" />
  <allow-intent href="*" subdomains="true"/>
  <access origin="*" subdomains="true"/> <!-- Required for iOS9 -->

我需要更改什么?已经在几天的时间里摔跤这个错误,这是一个有点烦人,无法实际创建新的酷功能在我的空闲时间。

What do I have to change? Been wrestling this bug for a few days off and on and it's a bit annoying to not be able to actually create new cool features in my free time.

编辑:我使用 phonegap serve 服务应用程序并使用PhoneGap开发人员应用程序测试。

I am serving the app using phonegap serve and testing it using the PhoneGap Developer App.

推荐答案

我建议您可能需要修改内容安全策略以包含一个connect-src子句,以指定Ajax请求可以转到哪里。

I would suggest your Content Security Policy might need to be modified to include a connect-src clause to specify where Ajax requests can go to.

标记:


< meta http-equiv =Content-Security-Policycontent = default-src *; style-src'self''unsafe-inline'; script-src:'self''unsafe-inline''unsafe-eval'/>

我建议修改这个开放Ajax请求到任何地方,看看是否有帮助,

I would suggest amending this to open up Ajax requests to anywhere, see if that helps then reign it in to just domain(s) you want to support after.

建议的CSP将是:


< meta http-equiv =Content-Security-Policycontent =default-src'self'data:gap:https://ssl.gstatic.com'unsafe-eval'; style-src' self''unsafe-inline'; media-src *; connect-src *>

如果这种方法有效,并且您想锁定到一个域后,您会需要:

If that works and you want to lock down to just one domain later you'd want something like:


< meta http-equiv =Content-Security-Policycontent =default-src'self'data:gap:https ://ssl.gstatic.com'unsafe-eval'; style-src'self''unsafe-inline'; media-src *; connect-src http://api.mydomain.com>

此外,我认为您需要更改应用程序的代码,以通过主机名或IP地址连接到您的服务器,在设备上,它不会将'localhost'与设备本身混淆,并尝试连接到设备上的端口6969.

Additionally I think you will need to change your app's code to connect to your server by hostname or IP address, so so that on the device it doesn't confuse 'localhost' with the device itself and try to make a connection to port 6969 on the device.

因此:


$ http.get(http:// localhost:6969 / random)

可能需要成为:


$ http $ http://myhost.mydomain.com:6969/random)


$ http.get(http://xxx.xxx.xxx.xxx:6969/random)

此在线上有一些资源:

  • Content Security Policy page
  • A blog post I wrote on this topic

这篇关于PhoneGap不想使用REST服务,无论我做什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆