请求的资源上没有'Access-Control-Allow-Origin'标头 - tomcat - restangular

时间:2014-10-01 16:26:05

标签: angularjs tomcat cors restangular

我无法理解为什么它不起作用。

当我使用邮递员时,这个过程很好,很完美。 GET,POST,PUT和DELETE,但是,当我使用restangular时,GET可以正常工作,但POST,PUT和DELETE都没有。

邮递员 http://x.x.x/testCORS/unit/1 - 返回200 ok

by restangular

app.config(['RestangularProvider',function(RestangularProvider) {
RestangularProvider.setBaseUrl('http://x.x.x:8086/testCORS');
RestangularProvider.setRestangularFields({
      selfLink: "_links.self.href"
    }); 

RestangularProvider.setDefaultHeaders({'Content-Type':'application/json'});
}]);

角度控制器

app
.controller('unitListCtrl',['$scope','Units','ngTableParams','$location', function($scope,Units,ngTableParams,$location) {

    $scope.remove = function( unit ) {

        unit.remove().then(function() {
                var index = $scope.tableParams.data.indexOf(unit);
                $scope.tableParams.data.splice(index, 1);               
    });                                                       
    };
}]);

我使用tomcat 7.0.41,web.xml是:

<filter>

<filter-name>CORS</filter-name>
<filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>    

<init-param>
    <param-name>cors.allowOrigin</param-name>
    <param-value>*</param-value>
</init-param>
<init-param>
    <param-name>cors.supportsCredentials</param-name>
    <param-value>false</param-value>
</init-param>
<init-param>
    <param-name>cors.supportedHeaders</param-name>
    <param-value>accept, authorization, origin</param-value>
</init-param>
<init-param>
    <param-name>cors.supportedMethods</param-name>
    <param-value>GET, POST, HEAD, OPTIONS, PUT, DELETE</param-value>
</init-param>

</filter>  


<filter>
  <filter-name>vraptor</filter-name>
  <filter-class>br.com.caelum.vraptor.VRaptor</filter-class>
</filter>  

<filter-mapping>
    <filter-name>CORS</filter-name>
    <url-pattern>/*</url-pattern>    
 </filter-mapping>


 <filter-mapping>
   <filter-name>vraptor</filter-name>
   <url-pattern>/*</url-pattern>
   <dispatcher>FORWARD</dispatcher>
   <dispatcher>REQUEST</dispatcher>

 </filter-mapping>

返回

Remote Address:[::1]:8086
Request URL:http://x.x.x:8086/testCORS/unit/1
Request Method:OPTIONS
Status Code:403 Forbidden
Request Headersview source
Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4
Access-Control-Request-Headers:accept, authorization, content-type
Access-Control-Request-Method:DELETE
Connection:keep-alive
Host:localhost:8086
Origin:http://localhost:8085
Referer:http://localhost:8085/testCORS/
User-Agent:Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.2 Safari/537.36
Response Headersview source
Content-Length:92
Content-Type:text/plain;charset=ISO-8859-1
Date:Wed, 01 Oct 2014 16:06:01 GMT
Server:Apache-Coyote/1.1
ConsoleSearchEmulationRendering

0 个答案:

没有答案