我想使用Angular和Cordova框架制作一个webapp。现在我已经启动并运行了所有内容,但我希望通过$ http.get方法获取json提要。在我的浏览器中,它完美地获取了json对象,但在我模拟的IOS上它返回零。
我阅读了有关Cordova 4.0更新的内容,它告诉我使用我连接的域更新白名单(Cordova插件白名单),但看起来我错过了一些东西。我的config.xml包含以下配置:
<allow-intent href="http://*/*"/>
<allow-intent href="https://*/*"/>
<allow-navigation href="http://*/*"/>
我的index.html如下:
<!DOCTYPE html />
<html>
<head>
<script type="text/javascript" src="assets/javascript/libs/angular/angular.js"></script>
<script type="text/javascript" src="assets/javascript/libs/angular-ui-router/release/angular-ui-router.js"></script>
<script type="text/javascript" src="assets/javascript/libs/angular-route/angular-route.js"></script>
<script type="text/javascript" src="assets/javascript/libs/angular-sanitize/angular-sanitize.js"></script>
<script type="text/javascript" src="assets/javascript/libs/angular-animate/angular-animate.js"></script>
<script src="assets/javascript/libs/ngCordova/dist/ng-cordova.js"></script>
<script src="cordova.js"></script>
<script type="text/javascript" src="assets/javascript/application/init.js"></script>
<script type="text/javascript" src="assets/javascript/navigation/navigation.js"></script>
<script type="text/javascript" src="assets/javascript/news/news.js"></script>
<link rel="stylesheet" href="assets/css/default.css"/>
<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://localhost/path-to-site">
</head>
<body ng-app="mk">
<nav-bar></nav-bar>
<div class="main-container">
<div class="wrapper">
<nav></nav>
<div class="view-animate-container">
<ng-view class="view-animate"></ng-view>
</div>
</div>
</div>
</body>
我的主页html如下:
<div ng-controller="homeNewsCtrl">
<div ng-repeat="item in homenewsitems track by $index">
<a class="newsitem" ng-href="#/newsitem?id={{item.ID}}">
<div class="newsimage" style="background-image: url('{{item.featured_image.source}}');"></div>
<h2 ng-bind-html="item.title"></h2>
<p class="meta">{{item.date | date:'dd-MM-yyyy / hh:mm'}}</p>
</a>
</div>
<a class="read-more" ng-click="loadMore(1)">Meer nieuws laden</a>
我的控制器如下:
app.controller('homeNewsCtrl', function ($scope, $http) {
$http.get('http://domain/wp-json/posts').then(function (response) {
$scope.homenewsitems = response.data;
console.log(response);
}, function error(response) {
alert(response.data);
});
$scope.count = 1;
$scope.loadMore = function (counter) {
$scope.count += counter;
$http.get('http://domain/wp-json/posts?page=' + $scope.count).then(function (response) {
$scope.homenewsitems.push.apply($scope.homenewsitems, response.data);
}, function error(response) {
alert(response.status)
});
};
});
答案 0 :(得分:0)
我认为您需要在内容安全策略元标记中添加“connect-src”子句,使其看起来更像这样:
<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://hostname.mysite.com">
根据您连接到这些服务器的方式,将http://hostname.mysite.com替换为您的服务器/服务器列表,并使用http / https。如果在iOS 9上运行,您可能还需要在项目plist文件中配置App Transport Security,如果访问非SSL安全服务器。我在这上面写了一个blog post的示例项目,可以帮助你。
答案 1 :(得分:0)
最终我在App Transport Security插件中找到了解决方案。
https://github.com/leecrossley/cordova-plugin-transport-security。在plugin.xml中,我可以管理要连接的主机。