离子 - 跨源请求被阻止的问题

时间:2014-12-10 13:55:41

标签: angularjs .htaccess cors ionic-framework

我使用Yeoman生成器(https://github.com/diegonetto/generator-ionic)设置了Ionic开发环境。一切都运作良好。

问题在于Sever端请求,我收到了跨源请求阻止错误。

我在.htaccess文件和AngularJS app.js中添加以下链接,仍然得到相同的错误

Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Headers: Authorization

AngularJS Provider

$httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
$httpProvider.defaults.withCredentials = true;

我收到的错误消息是

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at 
http://192.168.1.18/tracker/user/login. 
This can be fixed by moving the resource to the same domain or enabling CORS.

请求屏幕&响应标题

Screen of Request & Response Header

请指导我为启用CORS而需要更改的任何配置。我们的服务器端框架是Zend 1.x

3 个答案:

答案 0 :(得分:10)

您的浏览器强制使用交叉原点,不应使用当前设置阻止从任何分布式手机运行应用程序。

允许您与浏览器之间进行通信"应用程序到您的服务器使用浏览器插件

Allow-Control-Allow-Origin: * Chrome Plugin开展工作

答案 1 :(得分:1)

我已经更改了.htaccess文件,如下所示,谷歌浏览器不接受'*',所以我指定了确切的网址

Header set Access-Control-Allow-Origin "http://localhost:8100"
Header set Access-Control-Allow-Credentials: true

答案 2 :(得分:0)

虽然我没有使用Zend和Ionic的经验,但是当我通过Ionic与IIS托管的REST API进行交互时,我确实遇到过这个问题。

对我来说,除了你已经拥有的东西之外,我还必须添加“访问控制允许 - 方法:GET,POST,PUT,DELETE,OPTIONS”服务器端。

添加'allow methods'标题后,我能够充分利用我的API。

如果这不适合你,请回报一下,我会在我的项目中深入挖掘一下,看看我是否忘记了客户端的变化,这也是必需的。