【问题标题】:@DELETE not working in AngularJS + JAVA+CORS@DELETE 在 AngularJS + JAVA+CORS 中不起作用
【发布时间】:2014-06-15 13:19:58
【问题描述】:

我已经在 AngularJS 上玩了几个星期了......我跑到了一个角落...... 场景:

本地主机上的 Java REST(Resteasy):8080(您看到 company.com 是本地主机)

一个由 grunt 在 9001 上服务的 AnguljarJS APP

JAVA端正在使用CORS过滤器

<filter>
    <filter-name>CORS</filter-name>
    <filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>CORS</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

方法

@DELETE
@Path("/{id}")
public void delete(@PathParam("id")Long id);

如果我添加“@Consumes({ MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN })”没有任何区别

AngularJS 方面: 服务:

service.factory('SensorTypeService', function($resource){
return $resource('http://company.com:8080/hackstation-web/service/sensortype/:Id',
    {Id: '@Id'},
    {
        'update': {method: 'POST'},
        'remove': {method:'DELETE'},
        'reviews': {'method': 'GET', 'params': {'reviews_only': 'true'}, isArray: true}

    });

});

电话:

$scope.delete = function(id){
    SensorTypeService.remove({},{'Id': id}, function(){
        $scope.refresh();
    });
};

到目前为止,我发现了什么: 如果我运行: curl http://company.com:8080/hackstation-web/service/sensortype/9 -X 删除

在终端上,工作正常!!该方法在 JAVA SIDE 上被调用并被删除。

在我的应用程序中,我可以在萤火虫的控制台上看到所有的 GET POST 方法,但是 DELETE 没有显示出来。但我可以调试该方法,我可以看到从 angularjs-resource.js 到 angular.js 我可以看到该方法设置为 DELETE,Accept 是纯文本中的 json 类型......看起来像没关系...但不知何故,它只是不去!

我在这里阅读了很多类似的问题,尝试了很多,但一无所获。

据我所见,url 是按应有的方式创建的,accept 就在那里,方法是 DELETE。但不知何故,它只是出现在控制台上。

顺便说一句,所有其他方法查询、更新...一切正常!

有什么想法吗?

【问题讨论】:

    标签: javascript java angularjs cors


    【解决方案1】:

    默认情况下,CORSFilter 仅支持 GET、POST、HEAD、OPTIONS。您应该将 DELETE 添加到supportedMethods 列表中:

        <init-param>
            <param-name>cors.supportedMethods</param-name>
            <param-value>GET, POST, HEAD, OPTIONS, DELETE</param-value>
        </init-param>
    

    详情here

    【讨论】:

      猜你喜欢
      • 2018-11-06
      • 2014-03-28
      • 2014-05-03
      • 1970-01-01
      • 2021-07-31
      • 2013-11-06
      • 2015-03-16
      相关资源
      最近更新 更多