AngularJS无法加载资源:服务器响应状态为403()

时间:2018-12-11 11:19:27

标签: angularjs spring spring-mvc angularjs-ng-resource

前端代码

      var app = angular.module('myApp', ['ngResource']);
         app.controller('UserController', ['$scope', '$resource',function($scope,$resource) 
            {
         $scope.deleteRec = function(username)
                 {
                    User = $resource(
                            'delete/:username',
                            {},
                             {method:'DELETE', params: {username: '@username'}});
                    User.delete({username: $scope.myform.username}).$promise.then(function successCallback(response) 
                    {
                        $scope.Message = response;
                    }, function errorCallback(response) {

                    });

                    $scope.myform.username = "";
                    $scope.myform.phone="";
                    $scope.myform.email="";
                  };

         }]);
<table border="1" width="50%" height="50%"> 
    <tr><th>user_name</th><th>phone</th><th>email</th><th>delete</th></tr>
     <tr data-ng-repeat="user in usernames">
     <td><span data-ng-bind="user.username"></span></td>
      <td><span data-ng-bind="user.phone"></span></td>
       <td><span data-ng-bind="user.email"></span></td>
        <td><button data-ng-click="deleteRec(user.username)">delete</button>
       </tr>   
   </table>   

Spring Controller代码

@RequestMapping(value="/delete/{username}")
    @ResponseBody
    public String delete(@PathVariable String username) 
    {
        String user=retrievedataservice.delete(username);
        return user;
    }

如何通过angularjs请求删除特定记录。 Angularjs不会将参数发送到spring控制器

1 个答案:

答案 0 :(得分:0)

在angularjs中,请执行一次API调用:

$http({
  method: 'DELETE',
  url: 'delete/:username,
  data: {
    username: $scope.myform.username
  },
  headers: {
    'Content-type': 'application/json;charset=utf-8'
  }
}).then(function(response) {
    $scope.Message = response

});

在春季:

public String delete(@PathVariable('username') String username){}

@PathVariable用于从URI模板访问参数

或改为使用

public String delete(@RequestParam String username){}