您的位置:首页 > Web前端 > AngularJS

AngularJs表单提交

2017-12-15 12:37 459 查看

页面

<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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  angularjs