使用Tomcat的AngularJS DELETE导致不支持405 Request方法'DELETE'

时间:2015-10-10 11:43:22

标签: rest neo4j tomcat8 angularjs-resource

我正在使用带有Angular前端的Tomcat(8.0.27)上提供的Spring Data Neo4j Rest后端,所有这些都在http://localhost:8080/本地提供。当我尝试使用$ http.delete()或$ resource()。delete()删除记录时,tomcat返回405不支持的错误。

但是,使用$ http({method:'DELETE',url:'/ compet / 13'})可以正常工作。不幸的是,这不允许我使用angular $ resource服务而不是$ http,这是我的目标。 (另外,PUT创建与DELETE相同的405响应)

我也可以从命令行使用DELETE。这很好用:

curl -X DELETE http://localhost:8080/competitors/13

我在WebSecurityConfig中禁用了csrf,以防万一与它有关。

我还在web.xml中添加了readonly init-param,试图强制tomcat接受DELETE,如下所示:

<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<display-name>Proto v2 Application</display-name>

<servlet>
    <servlet-name>proto</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>readonly</param-name>
        <param-value>false</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>proto</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

由于我已经验证服务器能够执行删除,因此我将省略所有Java代码。这是我带有角度模块的完整.js文件:

var magneticApp = angular.module('magneticApp', ['ngResource'])
.controller('defaultCtrl', function ($scope, $http, $resource, baseUrl) {

    $scope.Competitor = $resource("/competitors/:id", {id: "@id"});

    $scope.listItems = function () {
        var query = $scope.Competitor.get(function () {
            console.log('inside query');
        });
        query.$promise.then(
            function successCallback(data) {
                $scope.competitors = data._embedded.competitors;
            },
            function errorCallback(data) {
                console.log('error:');
                console.dir(data);
            }
        );
    };

    /*
     * THIS DELETE FUNCTION RETURNS A 405 NOT SUPPORTED ERROR
     */
    $scope.deleteItem = function (item) {
        var query = $scope.Competitor.delete({}, item.itemId);
        query.$promise.then(
            function successCallback(data) {
                console.log('success:');
                $scope.competitors.splice($scope.competitors.indexOf(item), 1);
            },
            function errorCallback(data) {
                console.log('error:');
                console.dir(data);
            }
        );
    };

    /*
     * THIS DELETE FUNCTION WORKS PROPERLY
     */
    $scope.deleteItemWorksCorrectly = function (item) {
        $http({method: 'DELETE', url: '/competitors/' + item.itemId})
            .then(
            function successCallback(data) {
                $scope.competitors.splice($scope.competitors.indexOf(item), 1);
            },
            function errorCallback(data) {
                console.log('delete error');
                console.dir(data);
            }
        );
    };


    $scope.adjustActive = function (item) {
        item.id = item.itemId;
        console.log('adjustActive');
        console.dir(item);
        $http.post('/competitors', item)
            .success(function (data) {
                $http.get('/competitors').success(function (data) {
                    console.dir(data._embedded.competitors);
                    $scope.competitors = data._embedded.competitors;
                });
            });
    };


    $scope.addItem = function (itemName) {
        var item = {name: itemName};
        $http.post('/competitors', item)
            .success(function (data) {
                $http.get('/competitors').success(function (data) {
                    console.dir(data._embedded.competitors);
                    $scope.competitors = data._embedded.competitors;
                    //magneticApp.controller('defaultCtrl').$scope.competitors = data._embedded.competitors;
                });
            });
    };

    $scope.listItems();

});

有人可以对此有所了解吗?感谢。

0 个答案:

没有答案