离子问题没有'Access-Control-Allow-Origin' [英] ionic probleme No 'Access-Control-Allow-Origin'
问题描述
我正在研究离子应用程序。
我的问题是:当我尝试从服务器获取数据时我得到了这个:
I'm working on an ionic apps. My problem is : when i try to get data from server i got this :
XMLHttpRequest无法加载 https://mywebsite.com/api 。请求的资源上不存在Access-Control-Allow-Origin标头。因此,不允许访问 http:// localhost 。
XMLHttpRequest cannot load https://mywebsite.com/api. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.
我已经尝试将其添加到.htaccess:
标题集Access-Control-Allow-Origin:*
I already try to add this to .htaccess : Header set Access-Control-Allow-Origin: *
这到我的api页面(PHP):header('Access-Control-Allow-Origin:*');
And this to my api page (PHP) : header('Access-Control-Allow-Origin: *');
但仍然无法正常工作
$ http.get(url).success(function(response){...}
$http.get(url).success(function(response) {...}
推荐答案
将它放在你的PHP文件之上,如:
Put it on top of your PHP file like:
<?php
header("Access-Control-Allow-Origin: *");
// then your stuff goes here
?>
注意:与PHP头函数的所有使用一样,这必须在从服务器发送任何输出之前。
Note: as with all uses of the PHP header function, this must be before any output has been sent from the server.
这篇关于离子问题没有'Access-Control-Allow-Origin'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!