gpt4 book ai didi

apache - AngularJS : Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource

转载 作者:行者123 更新时间:2023-12-03 15:19:01 26 4
gpt4 key购买 nike

这是我的代码:

angular.module('option')
.factory('optionListService', ['$resource', function($resource) {
return $resource(HOST+'option/action/:id', {}, {
'get': {method:'GET'},
'save': {method:'POST'},
'query': {method:'GET', isArray:true},
'remove': {method:'DELETE'},
'delete': {method:'DELETE'}
});
}]);

这适用于 GET 请求,而不适用于 POST !

我使用 Apache 作为服务器并配置它:
<Limit GET HEAD POST PUT DELETE OPTIONS>
Order Allow,Deny
Allow from all
</Limit>
Header set Access-Control-Allow-Origin "*"

在我的 angularjs 中,我包含在模块应用程序的配置中:
delete $httpProvider.defaults.headers.common['X-Requested-With'];
delete $httpProvider.defaults.headers.post['Content-type'];

但请求 POST 仍然无法正常工作!

我希望有人可以给出任何想法。

最佳答案

这是服务器端的问题。如果您的应用程序使用的是 spring 框架。您可以使用过滤器方法修复它

@Override
protected void doFilterInternal(HttpServletRequest req, HttpServletResponse res, FilterChain chain) throws ServletException, IOException {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
res.setHeader("Access-Control-Max-Age", "3600");
res.setHeader("Access-Control-Allow-Headers", "X-PINGOTHER,Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers,Authorization");
res.addHeader("Access-Control-Expose-Headers", "xsrf-token");
if ("OPTIONS".equals(req.getMethod())) {
res.setStatus(HttpServletResponse.SC_OK);
} else {
chain.doFilter(req, res);
}
}

顺便说一句,您可以通过帖子 angularjs spring cross-origin request blocked 深入挖掘它

关于apache - AngularJS : Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23665056/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com