在预检响应中,Access-Control-Allow-Methods不允许使用方法DELETE

Zha*_* Yi 13 java jersey cors angularjs

我正在使用泽西作为我的restful api实现.在前端,我使用angularjs $ http服务来发出http请求.当我请求删除方法时,我总是得到以下错误.

"Method DELETE is not allowed by Access-Control-Allow-Methods in preflight response."  
Run Code Online (Sandbox Code Playgroud)

我读了一些文章,他们说我需要允许删除"Access-Control-Allow-Methods".我已经设置了如下的响应过滤器,但它仍然存在这样的问题.我还该怎么办?

@Provider
public class CORSResponseFilter implements ContainerResponseFilter {

    @Override
    public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {
        MultivaluedMap<String, Object> headers = responseContext.getHeaders();

        headers.add("Access-Control-Allow-Origin", "*");
        headers.add("Access-Control-Allow-Methods", "*");
    }
}
Run Code Online (Sandbox Code Playgroud)

下面是我提出请求的角度代码:

$http({
            method: 'DELETE',
            url: remoteUrl,
            headers : {'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
                'ACCESS_TOKEN' : $cookieStore.get("access_token")
            },
            data : $httpParamSerializer({
                'id':id
            })
        }).success(function(data,status,headers,config) {
            $scope.refreshDepartments();
            console.log(data);
            alert("success");
        }).error(function(data,status,headers,config){
            console.log(data);
            alert("error");
        });
Run Code Online (Sandbox Code Playgroud)

Zha*_* Yi 20

经过一些测试,我找到了解决方案.我把allow方法放在标题上,如下所示,然后就可以了.我不知道为什么"*"不起作用.

headers.add("Access-Control-Allow-Methods", "GET, POST, OPTIONS, PUT, DELETE");
Run Code Online (Sandbox Code Playgroud)