在Phonegap中定义对外部域的访问权限

时间:2014-11-06 18:02:51

标签: cordova

我有一个phonegapp应用程序,连接到heroku上托管的API。我使用Codio和Canary作为我的模拟器。当我加载谷歌浏览器开发工具来查看我的应用程序,它连接比我在网络选项卡中查看;它给了我这3个错误;

(index):98 GET http://pinball-spring.codio.io:35729/livereload.js?snipver=1 net::ERR_CONNECTION_TIMED_OUT

(index):1 XMLHttpRequest cannot load http://venuetoronto.herokuapp.com/api/v1/neighbourhoods. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://pinball-spring.codio.io:8100' is therefore not allowed access.

(index):1 XMLHttpRequest cannot load http://venuetoronto.herokuapp.com/api/v1/search/meta. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://pinball-spring.codio.io:8100' is therefore not allowed access.

我已经查看了phonegap / apache / cordova文档,它表示将域名列入白名单(这是我认为我正在尝试做的事情);进入config.xml文件,它显示了这一点;

<!--
  Define access to external domains.

  <access />            - a blank access tag denies access to all external resources.
  <access origin="*" /> - a wildcard access tag allows access to all external resource.

  Otherwise, you can specify specific domains:
-->

<access origin="http://127.0.0.1*"/> <!-- allow local pages -->

<!--
  <access origin="http://phonegap.com" /> - allow any secure requests to http://phonegap.com/
  <access origin="http://phonegap.com" subdomains="true" />  - same as above, but including subdomains, such as http://build.phonegap.com/  
  <access origin="http://phonegap.com" browserOnly="true" /> - only allows http://phonegap.com to be opened by the child browser.
-->

Image of the emulator screen

2 个答案:

答案 0 :(得分:0)

尝试添加此内容:

<access origin="http://pinball-spring.codio.io" />

<access origin="http://venuetoronto.herokuapp.com" />

答案 1 :(得分:0)

我也是在这种情况下大约四天,直到我终于找到了解决办法。 我知道你会在不同的地方看到它用以下内容编辑你的config.xml文件:

<access origin="*" />

但无论出于何种原因,在我将上述内容更改为:

之前,这对我没有用处。
<access origin=".*" /> 

一切都开始正常; -ajax能够访问外部主机 - 外部网页的链接也在打开 最后,这是适用于我的配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<widget xmlns   = "http://www.w3.org/ns/widgets"
    xmlns:gap   = "http://phonegap.com/ns/1.0"
    id          = "com.sucep.app"
    versionCode = "10" 
    version     = "1.0.0" >


<name>App Name</name>
<description>
    My description 
</description>

<author href="http://tormuto.com" email="support@tormuto.com">
    Tormuto
</author>

<access origin="*" />
<access origin=".*" /> 
<icon src="icon.png" />

<gap:config-file platform="android" parent="/manifest">
    <uses-permission name="android.permission.ACCESS_NETWORK_STATE" />
</gap:config-file>

<gap:config-file platform="android" parent="/manifest">
    <uses-permission name="android.permission.INTERNET" />
</gap:config-file>  

<gap:plugin name="com.simplec.plugins.network" />
<gap:plugin name="org.apache.cordova.inappbrowser" version="0.5.2" />
</widget>