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

486 views Asked by At

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

<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

1

There are 1 answers

0
Sudhir Ojha On

There are no issue with your server side code. Your ajax should be like following:

$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");     
     });