原始服务器拒绝处理请求,因为有效载荷的格式不受目标资源上此方法的支持。 控制器类
@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代码
<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 友善的,任何人都在Spring MVC中使用angular js讲述 CRUD应用程序教程
答案 0 :(得分: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");
});
答案 1 :(得分:0)
用@Consumes(MediaType.APPLICATION_JSON)
注释您的方法,这表明doRegister(...)
将接受 json 形式的有效负载。
如果有效载荷的格式不同,则可以使用MediaType
类中的适当值