CORS过滤器不是在春天REST工作与angularjs客户端 [英] CORS Filter not working in spring REST with angularjs client

查看:139
本文介绍了CORS过滤器不是在春天REST工作与angularjs客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个项目包含巫春REST服务提供JSON格式和angularJS客户巫消费服务和工作完美。现在,我创建一个其他网站的项目女巫只包含客户端(angularjs和HTML视图),但我不知道如何从这个项目中获得我的休息服务。我绑CORS过滤春季REST,但它不工作。我无法在客户端项目(HTML视图)的数据。
我仍然在控制台这个错误:

I created a project witch contains spring rest service that provide json format and angularJS Client witch consume the service and that work perfectly. Now I create an other web project witch contains just the client (angularjs and html views) but I don't know how to access my rest service from this project. I tied CORS filter for spring REST but it is not working. I can't get data in the the client project (html views). I still have this error in console :

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

这是我在客户端项目都做:

this is what I have done in the client project:

service.js:

service.js :

angular.module('workflowService', ['ngResource']).
    factory('Demande', function ($resource) {
        return $resource('/rest/demande/:id', {}, {
            'save': {method:'PUT'}
        });
    });

application.js中:

application.js :

angular.module('todoApp', ['workflowService']).
    config(['$routeProvider', function ($routeProvider) {
$routeProvider.
        when('/demande/list', {templateUrl:'views/demande-list.html', controller:DemandeListController}).
        when('/demande/new', {templateUrl:'views/demande-new.html', controller:DemandeNewController}).
        when('/demande/:id', {templateUrl:'views/demande-detail.html', controller:DemandeDetailController}).
        otherwise({redirectTo:'/demande/list'}); }]);

controller.js

controller.js

function DemandeListController($scope, $location, Demande) {
$scope.demandes = Demande.query();
$scope.gotoDemandeNewPage = function () {
    $location.path("/demande/new");
};
$scope.deleteDemande = function (demande) {
    demande.$delete({'id':demande.idDemande}, function () {
        $location.path('/');
    });
};}

这是我在剩下的项目都做:
SimpleCORSFilter.java

and this is what I have done in the rest project : SimpleCORSFilter.java

public class SimpleCORSFilter implements Filter {

@Override
public void init(FilterConfig arg0) throws ServletException {}

@Override
public void doFilter(ServletRequest req, ServletResponse resp,
        FilterChain chain) throws IOException, ServletException {
    // TODO Auto-generated method stub
    HttpServletResponse response=(HttpServletResponse) resp;

    response.setHeader("Access-Control-Allow-Origin", "*");
    response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
    response.setHeader("Access-Control-Max-Age", "3600");
    response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");

    chain.doFilter(req, resp);
}

@Override
public void destroy() {} }

ClassControleur.java:

ClassControleur.java :

    @RequestMapping(value = "/demande", method = RequestMethod.GET, produces = "application/json")
   public @ResponseBody List<Demande> list(HttpServletResponse response) {
       connecter();
       List<Demande> ListDemandes = new ArrayList<Demande>();
       ListDemandes = (List<Demande>)rb.getDemandeRepository().findAll() ;

      return ListDemandes  ;
   }

web.xml中:

web.xml :

  <filter>
   <filter-name>simpleCORSFilter</filter-name>
   <filter-class>
   com.springrestprojet.controller.SimpleCORSFilter
   </filter-class>
</filter>
 <filter-mapping>
  <filter-name>simpleCORSFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>

任何帮助吗?

推荐答案

终于,我找到了一个解决办法:我只是做一个小的修改来的网址:
的http://本地主机:8080:8080 / springprojet / REST / demande /:ID
 现在工作得很好!

finally, I found a solution: I just do a small modification to the URL : http://localhost:8080:8080/springprojet/rest/demande/:id. now that works fine!

这篇关于CORS过滤器不是在春天REST工作与angularjs客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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