离子问题没有“访问控制允许来源" [英] ionic problem 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:
I already try to add this to .htaccess:
<ifModule mod_headers.c>
Header set Access-Control-Allow-Origin: *
</ifModule>
这到我的 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.
这篇关于离子问题没有“访问控制允许来源"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!