Angular and CORS

user2239655

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. 

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.

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. 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);
    }
}

Thanks Quentin.

Quentin

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.

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Ajax Angular SSL CORS

来自分类Dev

CORS Angular JS

来自分类Dev

CORS 403 error Angular and Express

来自分类Dev

带有Angular的Nginx CORS

来自分类Dev

Angular,CORS和Spring的问题

来自分类Dev

Angular JS,Jetty CORS问题

来自分类Dev

Web API CORS Angular $资源

来自分类Dev

CORS 问题 - Angular / PHP / Apache

来自分类Dev

CORS Chrome 通配符 '*' Angular 8?

来自分类Dev

CORS失去会议中的Angular应用

来自分类Dev

在Angular + Scala Play上启用CORS

来自分类Dev

CORS 403错误Angular和Express

来自分类Dev

Angular Fullstack + CORS:XMLHttpRequest无法加载

来自分类Dev

POST上的CORS angular js + restEasy

来自分类Dev

在Angular + Scala Play上启用CORS

来自分类Dev

CORS Angular 2 拦截器

来自分类Dev

CORS 策略在 Angular/NGINX 上被阻止

来自分类Dev

被 CORS 策略阻止 Angular 和 Slim

来自分类Dev

Angular $ http.delete CORS错误(预检请求)

来自分类Dev

Angular $ resource和自托管WebAPI的CORS问题

来自分类Dev

带有angular.js $ resource和ServiceStack Servies的CORS

来自分类Dev

添加Stormpath Angular SDK后出现CORS问题

来自分类Dev

CORS-Angular和Express的http OPTIONS错误

来自分类Dev

使用Angular JS,Cors调用symfony 2 rest api

来自分类Dev

使用Angular从Appveyor Rest API获取CORS错误

来自分类Dev

在Angular中调用远程URL时发生CORS策略错误

来自分类Dev

Asp.net Core和Angular 7 Cors

来自分类Dev

Angular.js中的CORS解决方法$ HTTP POST?

来自分类Dev

在Angular中使用Parse Client Key会产生CORS错误