【问题标题】:400 Bad request while sending restful request using angularJS使用angularJS发送宁静请求时出现400错误请求
【发布时间】:2015-11-30 18:37:16
【问题描述】:

我正在使用 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;

     }

【问题讨论】:

  • 您的 PathVariable 不应该是 userIds 而不是 userId 吗?您的网址中有一个额外的“s”。
  • 不发帖时为什么要使用POSTPOST 需要一个身体。
  • 我正在通过获取 userId 更新实现类中的记录字段,这就是为什么使用 post。

标签: angularjs spring-mvc


【解决方案1】:

问题出在 Paqman 评论的“s”上。我删除了“s”,然后保存并尝试,得到了预期的正确结果。谢谢帕克曼。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-06
    • 2011-01-10
    • 2021-12-21
    • 2019-05-29
    • 2016-12-08
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    相关资源
    最近更新 更多