400使用angularJS发送restful请求时出现错误请求

时间:2015-11-30 09:08:16

标签: angularjs spring-mvc

我正在使用angularJs和rest控制器进行Web服务调用。我收到了400个不好的请求。当我提出更改用户状态的请求时,我收到错误。请找到以下js文件,控制器和UI代码。

Js控制器 -

var user=angular.module('userApp', ['ngAnimate']);

user.controller('userController',function($scope,$http){
    var urlBase=window.location.pathname;
    $scope.selection = [];
    $scope.toggle=true;
    $scope.statuses=['YES','NO'];
    $scope.sortType= 'name';       // set the default sort type
    $scope.sortReverse= false;     // set the default sort order
    $scope.searchUser= '';         // set the default search/filter term
    $http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";

    //get all users and display initially
    $http.get(urlBase+"/users")
        .success(function(data){
             $scope.users = data;
        });

    $scope.addUser = function addUser() {
        if($scope.firstName=="" || $scope.lastName=="" || $scope.email == "" || $scope.mobile == ""|| $scope.status == ""){
            alert("Insufficient Data! Please provide values for task name, description, priortiy and status");
        }
        else{
         $http.post(urlBase + '/users/insert/' +$scope.firstName+'/'+$scope.lastName+'/'+$scope.email+'/'+$scope.mobile+'/'+$scope.status)
            .success(function(data) {
             $scope.users = data;   
             $scope.firstName="";
             $scope.lastName="";
             $scope.email="";
             $scope.mobile="";
             $scope.status="";
             $scope.toggle='!toggle';            
            });
        }
        };

        $scope.archiveUser = function archiveUser(id){
            console.log(":: Value of id :"+id);
            $http.post(urlBase+'users/archive/'+id)
            .success(function(data) {
                $scope.users = data;
                console.log(":: Data updated and displayed.")
            })
            .failure(function(data){
              alert("Failed to archieve task ::"+data);
          });

        };
    });

用户界面中正在调用代码的部分,我使用ng-repeat显示值,然后单击调用archiveUser()的按钮。 Id值很好。

<button class="btn" ng-Click='archiveUser(x.id)'><i class="fa fa-archive"></i></button> 

Spring Controller用于处理请求 -

 @RequestMapping(value="users/archive/{userIds}",method = RequestMethod.POST,headers="Accept=application/json")
     public List<User> archiveUser(@PathVariable int userId) {  

      User user = new User();
      user=userService.getUserService(userId);
      List<User> users=userService.getAllUserService();
      return users;

     }

1 个答案:

答案 0 :(得分:0)

问题在于&#39;&#39;正如帕克曼评论的那样。我删除了&#39; s&#39;然后保存&amp;尝试过,得到了期待的正确结果。谢谢帕克曼。