交叉原始请求的角度资源帖子不起作用

时间:2014-09-03 03:26:37

标签: angularjs rest cors

我有一个角度js应用程序,我试图用它来调用使用Spring开发的REST服务。

我的登录服务

loginService.constant('baseUrl', 'http://127.0.0.1:8080/login');

loginService.factory('loginService', function(baseUrl, $resource, $http) {
var userInfo = {};

var loginResource = $resource(baseUrl);

return {
    login: function(data) {
        new loginResource(data).$save().then(function(response) {
            console.log('response : '+response);
        });         
    }
}
})

我的app.js

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

我的CORSFilter

public class CORSFilter implements Filter {

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

@Override
public void destroy() { }

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,                              FilterChain filterChain) throws IOException, ServletException {
    if (servletResponse instanceof HttpServletResponse) {
        HttpServletResponse alteredResponse = ((HttpServletResponse) servletResponse);
        addHeadersFor200Response(alteredResponse);
    }
    filterChain.doFilter(servletRequest, servletResponse);
}

private void addHeadersFor200Response(HttpServletResponse response) {
    response.addHeader("Access-Control-Allow-Origin", "*");
    response.addHeader("Access-Control-Allow-Methods", "Cache-Control, Pragma, Origin, Authorization, Content-Type, X-Requested-With");
    response.addHeader("Access-Control-Allow-Headers", "GET, PUT, OPTIONS, X-XSRF-TOKEN");
}

} 

以下是浏览器中显示的错误

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

0 个答案:

没有答案