【问题标题】:AngularJS Failed to load resource: the server responded with a status of 403 ()AngularJS 加载资源失败:服务器响应状态为 403 ()
【发布时间】:2018-12-11 11:19:27
【问题描述】:

前端代码

      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>   

弹簧控制器代码

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

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

【问题讨论】:

  • 您是否允许编写数据库规则?

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


【解决方案1】:

在 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){}

【讨论】:

  • 先生,参数用户名没有通过弹簧控制器
猜你喜欢
  • 2019-11-28
  • 1970-01-01
  • 1970-01-01
  • 2022-06-11
  • 2015-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-15
相关资源
最近更新 更多