0

源服务器拒绝为请求提供服务,因为负载的格式不受目标资源上此方法的支持。控制器类

@RequestMapping(value="/register", method = RequestMethod.POST)
    public ModelAndView doRegister(@RequestBody UserBean userBean, BindingResult result)
    {
        ModelAndView view = new ModelAndView("index");
        if(!result.hasFieldErrors())
        {
            if(retrieveService.insert(userBean) != null)
            {
                System.out.println("done");
                }
                }   
        return view;
        }

角度js代码

<script type="text/javascript">
var app = angular.module('myApp', []);
app.controller("UserController", ['$scope', '$http', function($scope, $http, httpPostService) {
 var self=this;
 $scope.insertData = function()
{
    alert($scope.userBean.username);
     $http({
         method: "POST",
         url: "register",
         username: $scope.userBean.username,
         phone:  $scope.userBean.phone,
         email: $scope.userBean.email,
         address: $scope.userBean.address,
         password: $scope.userBean.password
        }).then(function(response){
         console.log(response.status);
         console.log("in success");

     }, function(response){
         console.log(response.status);
         console.log("in fail");     
     });
};
}]);
</script>
<form method="post" action="register" name="myForm">
  <label for="username" class="control-label">First Name:</label>
  <input type="text" data-ng-model="userBean.username" class="form-control"  placeholder="Enter Firstname"/><br>
   <label for="phone" class="control-label">Phone:</label>
  <input type="text" data-ng-model="userBean.phone" class="form-control"  placeholder="Enter phone no."/><br>
  <label for="email" class="control-label">Email:</label>
  <input type="text" data-ng-model="userBean.email" class="form-control"  placeholder="Enter email"/><br>
  <label for="address" class="control-label">Address:</label>
  <input type="text" data-ng-model="userBean.address" class="form-control"  placeholder="Enter address"/><br>
  <label for="password" class="control-label">Password:</label>
  <input type="password" data-ng-model="userBean.password" class="form-control"  placeholder="Enter password"/><br>
   <button type="submit" data-ng-click="insertData()" class="btn btn-primary">Submit</button>
</form>

如何将数据发送到整个 bean 好心,任何人都用 Angular js讲述Spring MVC 中的CRUD 应用程序教程

4

1 回答 1

1

您的服务器端代码没有问题。您的 ajax 应如下所示:

$http({
         method: "POST",
         url: "register",
         data: $scope.userBean
        }).then(function(response){
         console.log(response.status);
         console.log("in success");

     }, function(response){
         console.log(response.status);
         console.log("in fail");     
     });
于 2018-11-01T10:19:08.037 回答