简体   繁体   English

HTTP状态415 –带角度js的Spring MVC中不支持的媒体类型错误

[英]HTTP Status 415 – Unsupported Media Type error in Spring MVC with angular js

The origin server is refusing to service the request because the payload is in a format not supported by this method on the target resource. 原始服务器拒绝为请求提供服务,因为有效载荷的格式不受目标资源上此方法的支持。 Controller class 控制器类

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

Angular js code Angular 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>

how to send data to the whole bean Kindly, anyone tells about CRUD application tutorial in Spring MVC with angular js 如何将数据发送到整个bean亲爱的,任何人都可以通过angular js讲述Spring MVC中的CRUD应用程序教程

There are no issue with your server side code. 您的服务器端代码没有问题。 Your ajax should be like following: 您的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");     
     });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM