角和CORS [英] Angular and CORS

查看:178
本文介绍了角和CORS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我书面方式应用具有角1.2,我试图打电话休息本地GlassFish 3.1 API。
我打电话正确的:

I am writting app with angular 1.2 and I am trying to call to REST API on local glassfish 3.1. I am calling right that:

app.factory("shopModel", function($resource){
return $resource('http://localhost:8080/Schedule-service/shops', {}, {
    query: 
    {method:'GET', 
        headers: {'Content-Type': 'application/json'} 
        params:{}}

    });
});

但我在铬得到一个错误。

But I get an error in my chrome.

XMLHttpRequest cannot load http://localhost:8080/Schedule-service/shops. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. 

我添加到我的应用程序的配置此code,但这并没有帮助:

I added to my app config this code but this haven't helped:

app.config(['$httpProvider', function($httpProvider) {
$httpProvider.defaults.useXDomain = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];
}]);

我不知道该怎么办。我将感谢每一个提示。

I don't know what to do. I will by thankful for every tip.

编辑。
我加headers_module到我的Apache WAMP。我加入到我的httpd.conf文件:

Edit. I added headers_module to my apache wamp. And I added to my httpd.conf file this:

<IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin: *
</IfModule>

但还是不起作用。任何建议??

But still don't work. Any suggestions??

EDIT2。
好吧,我解决这个问题。我已经添加了这个过滤器到我的Spring的web:

Edit2. Ok I resolve It. I've added this filter to my Spring web:

 public class CorsFilter extends OncePerRequestFilter {

 @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
            throws ServletException, IOException {
    response.addHeader("Access-Control-Allow-Origin", "*");
    response.addHeader("Access-Control-Allow-Methods",
            "GET, POST, PUT, DELETE, OPTIONS");
    response.addHeader("Access-Control-Allow-Headers",
            "origin, content-type, accept, x-requested-with, sid, mycustom, smuser");
        filterChain.doFilter(request, response);
    }
}

由于昆汀。

推荐答案

您需要指定访问控制允许来源:* 在服务器上,你都使得请求的的(即GlassFish服务器),而不是托管请求来自网页服务器。

You need to specify Access-Control-Allow-Origin: * on the server you are making the request to (i.e. the glassfish server), not the server hosting the page the request is coming from.

有关爱丽丝访问Bob的服务器,鲍勃有权授予许可。它会破坏的目的,如果爱丽丝能做到这一点。

For Alice to access Bob's server, Bob has to grant permission. It would defeat the purpose if Alice could do it.

这篇关于角和CORS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆