2015-10-07 35 views
0

我的應用程序,一個帶$ hhtp.get請求的簡單離子應用程序,在我的Android手機上的PhoneGap Build之後無法使用。當我調試它給我一個404錯誤。 當我使用ionic serve -p $ PORT命令在我的筆記本電腦上運行它時,它給了我一個CORS錯誤。 如果我激活CORS插件,它可以在筆記本電腦上的Chrome中運行。 我在c9(雲9)上編碼並在Adobe 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="[email protected]" 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> 

Cordo v白名單插件已安裝。

我使用攔截器添加以下標題嘗試:

<Access-Control-Allow-Origin : *> 

我讀過到處是科爾多瓦白名單應該採取的這雖然照顧......請幫助我!

+0

首先檢查是否通過鍵入CMD作爲離子插件LS –

+0

@Marc,在安裝項目中的科爾多瓦 - 插件,你的白名單config.xml,你有什麼白名單插件參考? – andre3wap

+0

嗨,Anil,這裏是插件列表...它存在:'com.ionic.keyboard 1.0.4「Keyboard」 cordova-plugin-console 1.0.1「Console」 cordova-plugin-device 1.0.1「設備「 cordova-plugin-splashscreen 2.1.0」Splashscreen「 cordova-plugin-whitelist 1.0.0」whitelist「' –

回答

0

這個解決方案對我來說只適用於移動設備,而不是網絡瀏覽器。請檢查您正在使用的移動設備作爲

<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> 

,並在控制器中的最後一步的代碼都會被它
更新HTML代碼

$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="[email protected]" 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> 
+0

嗨Anil ...你在建什麼應用程序? –

+0

我正在用Ionic框架構建應用程序@ Marc-AndréLanglais –

+0

如果你願意,我會上傳一個演示項目在github @ Marc-AndréLanglais –

0

解決!問題在於,通過離子Cli構建可以將所有內容連接在一起而不會出現問題。但是,adobe build.phonegap.com構建不會。無論插件是否已存在於項目中,您都必須在配置文件中指定插件。所以到config.xml文件中加入:<gap:plugin name="cordova-plugin-whitelist" source="npm" />,當然不要忘了添加xmlns:gap="http://phonegap.com/ns/1.0"

+0

澄清@ Marc-Andre所說的內容。使用* Phonegap Build *,您可以通過將「config.xml」添加到插件來添加插件。這意味着您不必爲每個插件添加多個插件,只需將它添加到'config.xml'中並將其保存在config.xml中即可。在線示例顯示了這一點,但通常不會讀取文檔。 – JesseMonroy650