【问题标题】:Angularjs-$ngResource: When I DELETE something by id, it shows 403 errorAngularjs-$ngResource:当我通过id删除某些东西时,它显示403错误
【发布时间】:2016-04-23 05:21:37
【问题描述】:

这是删除按钮:

<td><input type="button" value="delete" ng-click="deleteUser(u.id)"></td>

这里是JS文件:

var app = angular.module('MyApp', ['ngResource']);


app.factory('baseRequest', ["$resource", function ($resource) {

    return $resource("/apis/:method/:id", {method:'@method',id: '@id'}, {

        query: {method: 'get', isArray: false}

    });

}]);


app.controller("MyCtrl", ["$scope", "baseRequest", function ($scope, baseRequest) {

    $scope.users = [];


    $scope.fetchAllUsers = function () {

        $scope.users = baseRequest.query({method: "getPageData.req"});

        console.log($scope.users);
    };

    $scope.fetchAllUsers();


    /**
     *   here is the delete method
     * 
     **/

    $scope.deleteUser = function (id) {

        baseRequest.delete({method: "deleteUser.req", id: id}, function (response) {

            console.log(response);

        }, function (error) {

            console.log(error);

        });
    };

}]);

现在当我点击删除按钮时,它显示如下错误:403,访问指定资源已被禁止。

请求信息:

请求网址:http://localhost:8080/apis/deleteUser.req/2 请求方法:删除 状态码:403 远程地址:[[::1]]:8080

这是 SpringMVC 控制器:

@RequestMapping("/apis")
@Controller
public class UserController {

    @Autowired
    private UserDao userDao;


    @ResponseBody
    @RequestMapping(value = "/getPageData", method = RequestMethod.GET)
    public Page<User> getDatas(@RequestParam(value = "pageNo", required = false, defaultValue = "1") String No) {

        int pageNo = Integer.parseInt(No);

        if (pageNo < 0) {

            pageNo = 1;
        }

        return userDao.getPageData(pageNo);
    }

    @ResponseBody
    @RequestMapping(value = "/deleteUser/{id}", method = RequestMethod.DELETE)
    public String deleteUser(@PathVariable("id") Integer id) {

        System.out.println(id);

        if (userDao.deleteUser(id)) {

            return "1";

        } else {

            return "0";
        }

    }
}

【问题讨论】:

  • deleteUser.req 中的.req 是什么?这似乎是问题所在。
  • .req 只是请求的后缀。 dispatcher*.req

标签: angularjs spring-mvc


【解决方案1】:

看起来这是服务器端映射问题,但您可以尝试将不同的参数传递给 $resourcedelete 操作。尝试将其更改为:

baseRequest.delete({method: "deleteUser.req", id: id}, function (response) {})

baseRequest.delete({method: "deleteUser.req", id: id}, {}, function (response) {

【讨论】:

  • 同理。我可以在控制台中获取完整的网址,如下所示:请求网址:localhost:8080/apis/deleteUser.req/1
  • 那么这可能是您的服务器端映射问题。您是否尝试过在 chrome.google.com/webstore/detail/advanced-rest-client/… 这样的休息客户端中点击删除 URL?
  • 是的,我只是在浏览器上直接加载了网址localhost:8080/apis/deleteUser.req/1,服务器没有响应。所以,我认为这可能是服务器的问题。
  • 不,不要直接在浏览器中加载该 URL,因为您的该 URL 应该仅与 DELETE 请求一起使用,而直接在浏览器中点击该 URL 将导致它与 GET 请求跨度>
  • 我刚刚使用该插件在 chrome 中测试了该 URL。是的,你是对的,服务器端响应失败。我现在必须检查服务器端。
【解决方案2】:

我想我已经找到了解决这个问题的答案。关键是当你传输一个带后缀URL的参数时(比如/XX.req),那么就会出现这个错误:服务器端无法识别这个请求。

例如:

错误:localhost:8080/apis/deleteUser.req/2

右:localhost:8080/apis/deleteUser/2

也许我应该试试这个:(我还没有测试过) http://localhost:8080/apis/2/deleteUser.req

================================================ ========

在我测试了上面的想法后,它们都运行良好。当然,我们应该使用最后一个。

localhost:8080/apis/2/deleteUser.req

所以,我只是像这样交换 URL 参数的位置:

$resource("/apis/:id/:method/", {method:'@method',id: '@id'}

【讨论】:

    猜你喜欢
    • 2014-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-18
    相关资源
    最近更新 更多