Phonegap - 构建后出错404

时间:2016-02-01 22:48:46

标签: android cordova

我正在尝试发送HTTP GET请求,在我测试时一切正常。但是当我构建应用程序并尝试发送请求时,我收到404错误。

这是我的index.html

export class AppComponent { 
  constructor(@Query(SearchBar) children:QueryList<SearchBar>) {
    this.childcmp = children.first();
  }

  (...)
}

我是PhoneGap的新手,我确信这很简单,但我在谷歌上找不到任何东西。

提前致谢

2 个答案:

答案 0 :(得分:1)

您可能需要在index.html中设置内容安全策略元标记,因为Cordova / PhoneGap 5或更高版本将需要此标记。

示例如下(在index.html的head部分中):

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

为您尝试访问的服务器交换http://myserver。如果这是您的问题,您实际上可能会在JS控制台中收到此类错误消息:

Refused to connect to ‘http://myserver/ping' because it violates the following Content Security Policy directive: “default-src ‘self’ data: gap: https://ssl.gstatic.com ‘unsafe-eval’”. Note that ‘connect-src’ was not explicitly set, so ‘default-src’ is used as a fallback.

SecurityError:DOM异常18:试图突破用户代理的安全策略。

This post可能有助于理解内容安全政策。

答案 1 :(得分:0)

在这里,我假设您正在使用Phonegap版本“cli-5.2.0”并面临Android问题。要解决404问题,请在 config.xml 文件

中添加以下行
<gap:plugin name="cordova-plugin-whitelist" source="npm" />
<access origin="*" />
<allow-intent href="*" />
<allow-navigation href="*" />

白名单插件需要与Phonegap“cli-5.2.0”一起使用才能成功向任何服务器发出请求。 白名单插件的链接https://github.com/apache/cordova-plugin-whitelist

相关问题