为什么服务器删除我的标题

时间:2014-08-20 09:11:34

标签: java angularjs java-ee

我们正在开发一个带有angularjs前端和JavaEE后端的Web应用程序。

当我们尝试从前端联系后端时,我们收到错误,因为服务器位于另一个域而不是前端。我一直在阅读跨域请求,我认为服务器为响应设置了所需的标头。这是后端代码:

private void fixHeader(HttpServletResponse response) {
    response.addHeader("Access-Control-Allow-Origin", "http://localhost:8383");
    response.addHeader("Access-Control-Allow-Methods", "GET, PUT, POST, OPTIONS, DELETE");
    response.addHeader("Access-Control-Allow-Headers", "Content-type");
    response.addHeader("Access-Control-Max-Age", "86400");
    response.addHeader("Access-Control-Request-Headers", "X-Requested-With, accept, content-type");
    System.out.println(response.getHeaderNames().toString());
}

@Override
protected void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    fixHeader(resp);
}

并且继承了前端的http请求

doLogin功能

 this.doLogin = function(user) {

        var user = {"data": user};
        console.log("test before login http " + JSON.stringify(user));


        return $http({
            method: 'POST',
            url: 'http://localhost:8080/myInnovationBackend/Login',
            headers: {'Content-Type': 'application/json'},
            data: user
        })

    };

登录控制器:

 loginService.doLogin(user)
            .success(function (data) 
            {
               $scope.loginStatus = data;


                var message = $scope.loginStatus.data.status;
                console.log("test on get: " + message);

                if(message ==='ok'){
                $location.url('/loggedIn');
                console.log("OK")
                }
            })
            .error(function(data)   
            {
            console.log("test after error" + JSON.stringify(data));    
            return data ;
            });

就我在互联网上阅读而言,这应该足够了。但是当我们从前端进行登录时,它会到达错误函数。

XMLHttpRequest cannot load http://localhost:8080/myInnovationBackend/Login. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8383' is therefore not allowed access. frontPage.html:1
test after error""

以前有人有这个错误吗?

1 个答案:

答案 0 :(得分:0)

刚开始工作。我认为我误解了Options方法的概念。所以我已经将我的fixHeaders添加到doPost方法中,然后就可以了!

所以而不是

private void fixHeader(HttpServletResponse response) {
response.addHeader("Access-Control-Allow-Origin", "http://localhost:8383");
response.addHeader("Access-Control-Allow-Methods", "GET, PUT, POST, OPTIONS, DELETE");
response.addHeader("Access-Control-Allow-Headers", "Content-type");
response.addHeader("Access-Control-Max-Age", "86400");
response.addHeader("Access-Control-Request-Headers", "X-Requested-With, accept, content-type");
System.out.println(response.getHeaderNames().toString());
}

@Override
protected void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    fixHeader(resp);
}

我做了:

private void fixHeader(HttpServletResponse response) {
    response.addHeader("Access-Control-Allow-Origin", "http://localhost:8383");
    response.addHeader("Access-Control-Allow-Methods", "GET, PUT, POST, OPTIONS, DELETE");
    response.addHeader("Access-Control-Allow-Headers", "Content-type");
    response.addHeader("Access-Control-Max-Age", "86400");
    response.addHeader("Access-Control-Request-Headers", "X-Requested-With, accept, content-type");
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws     ServletException, IOException {
    fixHeader(response);
}

@Override
protected void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    fixHeader(resp);
}