页面
<body ng-app="myApp">
<form ng-submit="submitForm()" ng-controller="carController">
汽车品牌:<input type="text" ng-model="car.brand"/> <br/>
汽车颜色:<input type="text" ng-model="car.color"/> <br/>
<button type="submit">submit</button>
</form>
<script>
var app = angular.module("myApp",[]);
app.controller('carController',function ($scope,$http) {
$scope.submitForm = function () {
console.log('enter submitForm');
$http({
method:'post',
url:'/addCar',
data:$.param($scope.car),
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}).then(function(resp){
console.log('post success');
console.log(resp);
},function(resp){
console.log('post error');
console.log(resp);
});
}
});
</script>
</body>
后台
@Controller
@RequestMapping
public class CarController {
@RequestMapping("/addCar")
@ResponseBody
public Car addCar(Car car){
if(car!=null) car.setId(101L);
System.out.println("enter addCar");
System.out.println("car:"+car);
return car;
}
}