2016-04-23 62 views
1

这是删除按钮:

<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,禁止访问指定的资源。

请求信息:

请求URL:http://localhost:8080/apis/deleteUser.req/2 请求方法:DELETE 状态代码: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"; 
     } 

    } 
} 
+0

'deleteUser.req'中的'.req'?似乎是问题 –

+0

.req只是请求的后缀。 \t \t 调度程序 \t \t * .req \t JSO

回答

0

看起来像是服务器端映射问题,但您可以尝试将不同的参数传递给动作$resource。尝试对其进行更改:

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

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

相同。我可以在控制台中获取完整的URL,如下所示:请求URL:http:// localhost:8080/apis/deleteUser.req/1 – JSO

+0

然后,它可能是您的服务器端映射问题。您是否尝试在https://chrome.google等其他客户端中点击删除网址?COM /网络存储器/详细信息/高级休息客户端/ hgmloofddffdnphfgcellkdfbfbjeloo? –

+0

是的,我只是在浏览器上直接加载了URL http:// localhost:8080/apis/deleteUser.req/1,服务器没有任何回应。所以,我认为这可能是服务器的问题。 – JSO

0

我想我已经找到了答案来解决这个问题。关键在于,当您传输带有后缀URL的参数(如/XX.req)时,会发生此错误:服务器端无法识别此请求。

例如:

错误:本地主机:8080 /的API/deleteUser.req/2

权:本地主机:8080 /的API/deleteUser/2

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

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

I t上面提到了这些想法,两者都运作良好。当然,我们应该使用最后一个。

本地主机:8080 /的API/2/deleteUser.req

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

$资源(“/的API /:ID /:方法/ “,{method:'@ method',id:'@id'}