在Ionic中设置CORS [英] Setting up CORS in Ionic
问题描述
我正在创建自己的第一个离子应用程序,但是我在cors方面苦苦挣扎,在这一领域我经验不足,所以这个问题似乎很愚蠢.
I'm in the process of creating my first ionic app, but I'm struggling with cors, I don't have a lot of experience on this area, so this question may seem stupid.
我无权访问api网站,因此我无法在此进行任何更改. 这是我用来从api网站获取数据的代码.我已经读到cors只是在使用离子服务进行测试时出现的一个问题,但是我尝试构建android应用并将apk转移到手机上,但是它似乎也无法正常工作(而且我不能看到我的手机话机上的控制台日志进行测试:))
I do not have access to the api site, so I can't change anything there. This is the code I am using to get the data from the api site. I've read that the cors is only a problem when testing with ionic serve, but I've tried building the android app and transferring the apk to my phone, but it doens't seem to work there either (and I can't see the console log on my phone offcourse to test it :) )
app.controller('VolleyCtrl', function($http, $scope) {
$scope.wedstrijden = [];
$http.get('http://www.volleyadmin2.be/services/wedstrijden_xml.php?province_id=5&stamnummer=O-0696&format=json')
.success(function(response) {
angular.forEach(response, function(child) {
$scope.wedstrijden.push(child);
})
});
});
推荐答案
如果您使用更新版本的Cordova(或最新的Ionic CLI)开发应用程序,则当您的应用程序尝试发出网络请求时,您可能会遇到http 404错误.
If you're using a newer version of Cordova (or the latest Ionic CLI) to develop your app, you may be experiencing http 404 errors when your app tries to make network requests.
可以使用科尔多瓦白名单插件快速解决.
您可以在离子文档:科尔多瓦白名单中找到更多文档.
You can find more documentation on Ionic docs: Cordova Whitelist.
解决方案:
在外壳程序/终端中运行以下命令:
Run the following command in your shell/terminal:
ionic plugin add https://github.com/apache/cordova-plugin-whitelist.git
您现在唯一需要做的就是将一个属性添加到您的config.xml
文件:
The only thing you need to do now is to add a property to your config.xml
file:
<allow-navigation href="*" />
这篇关于在Ionic中设置CORS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!