在飞行前响应中,Access-Control-Allow-Methods 始终不允许方法删除
Always got Method DELETE is not allowed by Access-Control-Allow-Methods in preflight response
我正在使用球衣作为我的 restful api 实现。在前端,我使用 angularjs $http 服务来发出 http 请求。当我请求删除方法时,我总是遇到以下错误。
"Method DELETE is not allowed by Access-Control-Allow-Methods in preflight response."
我读了一些文章,他们说我需要在 "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", "*");
}
}
下面是我提出请求的 angular 代码:
$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");
});
Try using the CORS extension for chrome, it helped me once.
编辑
发生这种情况是因为 angular 在您的请求 headers 中添加了 X-Header 个键 headers。
X-Headers generally define or set the operating parameters for the HTTP req/res
您可以通过将请求的 Content-Type 修改为 "text/plain" 或 "application/x-www-form-urlencoded"[=33 来解决此问题=] 或 "multipart/form-data".
You can do this by using an interceptor while in your app config.
编辑
将此添加到您的服务器代码中 -
header('Access-Control-Allow-Headers: X-Requested-With');
Try changing the content-type to text/plain
希望这对您有所帮助。
经过一番测试,我找到了解决办法。我将 allow 方法放在 header 上,如下所示,然后它就可以工作了。我不知道为什么“*”不起作用。
headers.add("Access-Control-Allow-Methods", "GET, POST, OPTIONS, PUT, DELETE");
对于没有凭据的请求(没有 HTTP cookie 或 HTTP 身份验证信息的请求),值“*”仅算作特殊通配符值。在带有凭据的请求中,它被视为没有特殊语义的文字方法名称“*”。
来源:https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Methods
我正在使用球衣作为我的 restful api 实现。在前端,我使用 angularjs $http 服务来发出 http 请求。当我请求删除方法时,我总是遇到以下错误。
"Method DELETE is not allowed by Access-Control-Allow-Methods in preflight response."
我读了一些文章,他们说我需要在 "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", "*");
}
}
下面是我提出请求的 angular 代码:
$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");
});
Try using the CORS extension for chrome, it helped me once.
编辑
发生这种情况是因为 angular 在您的请求 headers 中添加了 X-Header 个键 headers。
X-Headers generally define or set the operating parameters for the HTTP req/res
您可以通过将请求的 Content-Type 修改为 "text/plain" 或 "application/x-www-form-urlencoded"[=33 来解决此问题=] 或 "multipart/form-data".
You can do this by using an interceptor while in your app config.
编辑
将此添加到您的服务器代码中 -
header('Access-Control-Allow-Headers: X-Requested-With');
Try changing the content-type to text/plain
希望这对您有所帮助。
经过一番测试,我找到了解决办法。我将 allow 方法放在 header 上,如下所示,然后它就可以工作了。我不知道为什么“*”不起作用。
headers.add("Access-Control-Allow-Methods", "GET, POST, OPTIONS, PUT, DELETE");
对于没有凭据的请求(没有 HTTP cookie 或 HTTP 身份验证信息的请求),值“*”仅算作特殊通配符值。在带有凭据的请求中,它被视为没有特殊语义的文字方法名称“*”。
来源:https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Methods