[英]$http get in ionic not working on real device even cordova-plugin-whitelist is installed
I am creating an ionic app that fetch json value from remote server using $http get. 我正在创建一个离子应用程序,该应用程序使用$ http get从远程服务器获取json值。 It displays the data when I run it on the browser but not on real Android device. 当我在浏览器上而不是在真正的Android设备上运行数据时,它将显示数据。 I also have cordova-plugin-whitelist
installed already. 我也已经安装了cordova-plugin-whitelist
。
Here is my code on the index.html
这是我在index.html
上的代码
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'">
<title></title>
<link href="lib/ionic/css/ionic.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
<!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
<link href="css/ionic.app.css" rel="stylesheet">
-->
<!-- ionic/angularjs js -->
<script src="lib/ionic/js/ionic.bundle.js"></script>
<!-- cordova script (this will be a 404 during development) -->
<script src="cordova.js"></script>
<!-- your app's js -->
<script src="js/app.js"></script>
<script src="js/controllers.js"></script>
</head>
<body ng-app="starter">
<ion-nav-view>
</ion-nav-view>
</body>
</html>
controller.js
.controller('ProductsCtrl', function($scope, $ionicModal, $timeout, $http, $stateParams) {
console.log("ProductCtrl: prod_id = " + $stateParams.prod_id);
$scope.host = "xxxx";
$http.get("http://"+ $scope.host +"/xxxx/xxxx.php?cat_id=" + $stateParams.prod_id)
.then(function(response) {
$scope.products = response.data;
})
.error(function(data, status, headers,config){
console.log('data error');
})
.then(function(result){
things = result.data;
});
})
config.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<widget id="com.ionicframework.xxxxxxx" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
<name>XXXX</name>
<description>
An Ionic Framework and Cordova project.
</description>
<author email="you@example.com" href="http://example.com/">
Your Name Here
</author>
<content src="index.html"/>
<access origin="*"/>
<preference name="webviewbounce" value="false"/>
<preference name="UIWebViewBounce" value="false"/>
<preference name="DisallowOverscroll" value="true"/>
<preference name="SplashScreenDelay" value="2000"/>
<preference name="FadeSplashScreenDuration" value="2000"/>
<preference name="android-minSdkVersion" value="16"/>
<preference name="BackupWebStorage" value="none"/>
<feature name="StatusBar">
<param name="ios-package" value="CDVStatusBar" onload="true"/>
</feature>
<allow-navigation href="http://xxxx.com/*" />
</widget>
As you can see in index.html
I've put the <meta>
tag as suggested on other post that I saw here 正如您在index.html
中看到的那样,我已经按照在此处看到的其他文章中的建议放置了<meta>
标记
<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'">
I also tried to uninstall and reinstall the cordova-plugin-whitelist
but nothing has changed. 我也尝试卸载并重新安装cordova-plugin-whitelist
但没有任何改变。
cordova plugin remove cordova-plugin-whitelist
cordova plugin add cordova-plugin-whitelist
These are the errors displayed on the console. 这些是控制台上显示的错误。 Mostly are just 404 like this: 大多只是这样的404:
Failed to load resource: net::ERR_NAME_NOT_RESOLVED
But this one is probably because of the <meta>
.. 但这可能是因为<meta>
..
Refused to load the script 'http://localhost:35729/livereload.js?snipver=1' because it violates the following Content Security Policy directive: "script-src 'self' 'unsafe-inline' 'unsafe-eval'".
(anonymous function) @ localhost:8100/?ionicplatform=ios:98
Please help.. thanks in advance.. 请帮助..在此先感谢..
I use this as my meta tag and it handles all such things. 我将其用作元标记,它可以处理所有此类事情。 Try this a let me know it it works 试试这个让我知道它有效
<meta http-equiv="Content-Security-Policy" content="default-src *; img-src *;style-src * 'unsafe-inline'; script-src * 'unsafe-inline' 'unsafe-eval'">
I have found the answer last time but I'll just post it now. 我上次找到了答案,但我现在将其发布。 I usually run my android app using Ionic Lab but when I run my app from console ( ionic run android
), I've noticed this: 我通常使用Ionic Lab运行我的android应用程序,但是当我从控制台( ionic run android
)运行我的应用程序时,我注意到了这一点:
"Using this version of Cordova with older version of corodva-android is being deprecated. Consider upgrading to cordova-android@5.0.0 or newer. " “不建议将此版本的Cordova与旧版本的corodva-android一起使用。请考虑升级到cordova-android@5.0.0或更高版本。”
so I just upgraded my cordova: 所以我刚升级了科尔多瓦:
cordova platform remove android
cordova platform add android@5.X.X
Then it worked finally. 然后终于奏效了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.