【问题标题】:Ionic $http error 404 using cordova whitelist使用科尔多瓦白名单的离子 $http 错误 404
【发布时间】:2015-10-07 08:29:00
【问题描述】:

我的应用程序是一个带有 $hhtp.get 请求的简单离子应用程序,在我的 Android 手机上的 PhoneGap Build 之后无法运行。当我调试它给我一个 404 错误。 当我使用 ionic serve -p $PORT 命令在笔记本电脑上运行它时,它给了我一个 CORS 错误。 如果我激活 CORS 插件,它确实可以在我的笔记本电脑上的 Chrome 中使用。 我在 c9 (cloud 9) 上编码并在 Adob​​e PhoneGap 上构建。

我的请求

.controller('teamCtrl', function($scope, $http, $window) {
  $http.get("http://google.com")
    .success(function(response) {
      $scope.categories = getArray(response);
      var b =1;
    })
    .error(function(response) {
      $window.alert("d'oh!"); 
    });
})

我在 index.html 中的 CSP:

<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src * 'unsafe-inline' 'unsafe-eval'">

config.xml:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<widget id="com.ionicframework.openorganix919048" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
  <name>OpenOrganix</name>
  <description>
        An Ionic Framework and Cordova project.
    </description>
  <author email="hi@ionicframework" href="http://ionicframework.com/">
      Ionic Framework Team
    </author>
  <content src="index.html"/>
  <access origin="*" />
  <allow-navigation href="http://*/*" />
  <allow-intent href="http://*/*"/>
  <preference name="webviewbounce" value="false"/>
  <preference name="UIWebViewBounce" value="false"/>
  <preference name="DisallowOverscroll" value="true"/>
  <preference name="android-minSdkVersion" value="16"/>
  <preference name="BackupWebStorage" value="none"/>
  <preference name="SplashScreen" value="screen"/>
  <preference name="SplashScreenDelay" value="3000"/>
  <feature name="StatusBar">
    <param name="ios-package" value="CDVStatusBar" onload="true"/>
  </feature>
  <platform name="android">
    <icon src="resources/android/icon/drawable-ldpi-icon.png" density="ldpi"/>
    <icon src="resources/android/icon/drawable-mdpi-icon.png" density="mdpi"/>
    <icon src="resources/android/icon/drawable-hdpi-icon.png" density="hdpi"/>
    <icon src="resources/android/icon/drawable-xhdpi-icon.png" density="xhdpi"/>
    <icon src="resources/android/icon/drawable-xxhdpi-icon.png" density="xxhdpi"/>
    <icon src="resources/android/icon/drawable-xxxhdpi-icon.png" density="xxxhdpi"/>
    <splash src="resources/android/splash/drawable-land-ldpi-screen.png" density="land-ldpi"/>
    <splash src="resources/android/splash/drawable-land-mdpi-screen.png" density="land-mdpi"/>
    <splash src="resources/android/splash/drawable-land-hdpi-screen.png" density="land-hdpi"/>
    <splash src="resources/android/splash/drawable-land-xhdpi-screen.png" density="land-xhdpi"/>
    <splash src="resources/android/splash/drawable-land-xxhdpi-screen.png" density="land-xxhdpi"/>
    <splash src="resources/android/splash/drawable-land-xxxhdpi-screen.png" density="land-xxxhdpi"/>
    <splash src="resources/android/splash/drawable-port-ldpi-screen.png" density="port-ldpi"/>
    <splash src="resources/android/splash/drawable-port-mdpi-screen.png" density="port-mdpi"/>
    <splash src="resources/android/splash/drawable-port-hdpi-screen.png" density="port-hdpi"/>
    <splash src="resources/android/splash/drawable-port-xhdpi-screen.png" density="port-xhdpi"/>
    <splash src="resources/android/splash/drawable-port-xxhdpi-screen.png" density="port-xxhdpi"/>
    <splash src="resources/android/splash/drawable-port-xxxhdpi-screen.png" density="port-xxxhdpi"/>
  </platform>
</widget>

Cordova 白名单插件已安装。

我尝试使用拦截器添加以下标头:

<Access-Control-Allow-Origin : *>

我到处读到 Cordova Whitelist 应该解决这个问题...请帮助我!

【问题讨论】:

  • 首先检查您是否在项目中安装了cordova-plugin-whitelist,方法是输入cmd as ionic plugin ls
  • @Marc,在你的 config.xml 中,你有什么 whitelist 插件参考?
  • 嗨 Anil,这是插件列表...它存在:com.ionic.keyboard 1.0.4 "Keyboard" cordova-plugin-console 1.0.1 "Console" cordova-plugin-device 1.0.1 "Device" cordova-plugin-splashscreen 2.1.0 "Splashscreen" cordova-plugin-whitelist 1.0.0 "Whitelist"
  • @andre3wap,谢谢,但我不确定您所说的引用是什么意思。你能举个例子吗? - 我在顶部添加了整个 config.xml。

标签: android cordova xmlhttprequest ionic phonegap-build


【解决方案1】:

此解决方案仅适用于不在网络浏览器上的移动设备。如果您使用移动设备的 as

,请检查它
更新您的 html 代码
<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'">

创建一个按钮来测试它

<button class="button button-bar button-balanced" ng-click="whitelist()">Test it</button>

控制器中的最后一步代码将是

$scope.whitelist = function(){

    console.log('method called');

    $http.get("https://www.google.co.in/").then(function(data){
      console.log(data);
      $scope.categories = data.status;
      console.log($scope.categories);
     }, function(error){
      console.log(error);
      $window.alert("d' oh!");
     });

  }

在您的 config.xml 文件中,代码必须是这样的

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<widget id="com.ionicframework.rangeslider745642" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
  <name>rangeslider</name>
  <description>
        An Ionic Framework and Cordova project.
    </description>
  <author email="hi@ionicframework" href="http://ionicframework.com/">
      Ionic Framework Team
    </author>
  <content src="index.html"/>
  <access origin="*"/>
  <preference name="webviewbounce" value="false"/>
  <preference name="UIWebViewBounce" value="false"/>
  <preference name="DisallowOverscroll" value="true"/>
  <preference name="android-minSdkVersion" value="16"/>
  <preference name="BackupWebStorage" value="none"/>
  <preference name="SplashScreen" value="screen"/>
  <preference name="SplashScreenDelay" value="3000"/>
  <feature name="StatusBar">
    <param name="ios-package" value="CDVStatusBar" onload="true"/>
  </feature>
  <platform name="android">
    <icon src="resources/android/icon/drawable-ldpi-icon.png" density="ldpi"/>
    <icon src="resources/android/icon/drawable-mdpi-icon.png" density="mdpi"/>
    <icon src="resources/android/icon/drawable-hdpi-icon.png" density="hdpi"/>
    <icon src="resources/android/icon/drawable-xhdpi-icon.png" density="xhdpi"/>
    <icon src="resources/android/icon/drawable-xxhdpi-icon.png" density="xxhdpi"/>
    <icon src="resources/android/icon/drawable-xxxhdpi-icon.png" density="xxxhdpi"/>
    <splash src="resources/android/splash/drawable-land-ldpi-screen.png" density="land-ldpi"/>
    <splash src="resources/android/splash/drawable-land-mdpi-screen.png" density="land-mdpi"/>
    <splash src="resources/android/splash/drawable-land-hdpi-screen.png" density="land-hdpi"/>
    <splash src="resources/android/splash/drawable-land-xhdpi-screen.png" density="land-xhdpi"/>
    <splash src="resources/android/splash/drawable-land-xxhdpi-screen.png" density="land-xxhdpi"/>
    <splash src="resources/android/splash/drawable-land-xxxhdpi-screen.png" density="land-xxxhdpi"/>
    <splash src="resources/android/splash/drawable-port-ldpi-screen.png" density="port-ldpi"/>
    <splash src="resources/android/splash/drawable-port-mdpi-screen.png" density="port-mdpi"/>
    <splash src="resources/android/splash/drawable-port-hdpi-screen.png" density="port-hdpi"/>
    <splash src="resources/android/splash/drawable-port-xhdpi-screen.png" density="port-xhdpi"/>
    <splash src="resources/android/splash/drawable-port-xxhdpi-screen.png" density="port-xxhdpi"/>
    <splash src="resources/android/splash/drawable-port-xxxhdpi-screen.png" density="port-xxxhdpi"/>
  </platform>
</widget>

【讨论】:

  • 嗨 Anil... 你用什么构建应用程序?
  • 我正在使用 Ionic 框架 @Marc-AndréLanglais 构建应用程序
  • 如果你愿意,我会在github上传一个demo项目@Marc-AndréLanglais
  • 就像我在帖子中提到的,我正在使用 PhoneGap 构建,因为由于我的帐户空间限制,我无法在 cloud 9 上安装 android SDK。
  • 该应用程序在我的本地机器上使用 ionic 编译时可以工作......所以我认为我们可以将这个记录到 Adob​​e PhoneGap Build 问题中。非常感谢 Anil。
【解决方案2】:

解决了!!问题是通过 ionic cli 的构建会将所有内容毫无问题地链接在一起。但是,adobe 构建的 build.phonegap.com 没有。无论插件是否已存在于项目中,您都必须在配置文件中指定插件。所以在 config.xml 文件中添加:&lt;gap:plugin name="cordova-plugin-whitelist" source="npm" /&gt;当然不要忘记添加xmlns:gap="http://phonegap.com/ns/1.0"

【讨论】:

  • 澄清@Marc-Andre 所说的话。使用 Phonegap Build,您可以通过添加到 config.xml 来添加插件。这意味着您不必为每个插件添加多个,只需在config.xml 中添加一次并在 config.xml 中维护它。在线示例显示了这一点,尽管通常不阅读文档。
猜你喜欢
  • 2017-12-28
  • 1970-01-01
  • 1970-01-01
  • 2017-05-20
  • 1970-01-01
  • 2017-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多