离子问题否“访问控制允许来源" [英] 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屋!