Harshit
Harshit

Reputation: 117

400 Bad request while sending restful request using angularJS

I am using angularJs and rest controllers for making web service call. I am getting 400 bad request. When I was making a request to change the status of the user then I am getting the error. Please find the below code for js file, controller and UI.

Js Controller-

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);
          });

        };
    });

A part in User interface where the code is being called , i am using ng-repeat for displaying values and then on click of button calling archiveUser(). Id value is coming fine.

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

Spring Controller for handling request-

 @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;

     }

Upvotes: 0

Views: 461

Answers (1)

Harshit
Harshit

Reputation: 117

The problem was with 's' as Paqman commented. I removed 's' and then save & tried, got the correct result that was expecting. Thanks Paqman.

Upvotes: 0

Related Questions