Angularjs 如何使用Post方法来获取角度信息
无法使用mysql anglerJS和Spring控制器将数据插入数据库。给出400个错误。我试着用谷歌搜索,但没有得到代码。所以我犯了错误 Controller.javaAngularjs 如何使用Post方法来获取角度信息,angularjs,http,spring-mvc,Angularjs,Http,Spring Mvc,无法使用mysql anglerJS和Spring控制器将数据插入数据库。给出400个错误。我试着用谷歌搜索,但没有得到代码。所以我犯了错误 Controller.java @RequestMapping(value="/insert",method=RequestMethod.POST) public Emp insret(@RequestParam(value="user")String user,@RequestParam(value="pass1")String pass) {
@RequestMapping(value="/insert",method=RequestMethod.POST)
public Emp insret(@RequestParam(value="user")String
user,@RequestParam(value="pass1")String pass)
{
Emp em=new Emp();
em.setUser(user);
em.setPass(pass);
System.out.println(user+""+pass);
return er.save(em);
}
用于post方法的角度文件
script1.js
var app = angular.module("myApp", [])
.controller("myCtrl", function ($scope, $http, $log) {
$scope.abc = function () {
$log.info($scope.user)
$log.info($scope.pass1)// you will have your updated value here
$http({
method:'POST',
url: '/insert',
data : {
'user' : $scope.user,
'pass':$scope.pass1
},
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
})
.then(function (response) {
$scope.repos = response.data;
$log.info(response);
});
}
});
嵌入在HTML文件中的post方法
index.html
<!DOCTYPE html>
<html>
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js">
</script>
<script type="text/javascript" src="script1.js"></script>
</head>
<body ng-app="myApp" >
<div ng-controller="myCtrl">
<form ng-submit="abc()">
<input type="text" ng-model="user" />
<input type="text" ng-model="pass1" />
<button type="submit">Test</button>
</form>
</div>
</body>
</html>
正式答复:
@PostMapping(value="/insert",produces="application/json")
public Emp insret(@RequestBody Emp em){
//do ....
return er.save(em);
}
当您发布来自Angular或其他客户机的数据时,这些数据都在请求主体中,这就是Spring人员引入此注释的原因
@RequestParam
用于检索请求参数值,在您的情况下,这些值应该是:/insert?user=value1&pass1=value2
,这不是您想要的。只是想知道,您是否尝试过将“application/json”作为内容类型?POST 400()当我使用“application/json”没有web上下文路径时,我遇到了这个错误?如果服务器接受内容类型作为应用程序/x-www-form-urlencoded
,您是否尝试序列化数据?例如使用数据:$.param({user:$scope.user,pass:$scope.pass1})
@shookuancs,我尝试不插入它
@PostMapping(value="/insert",produces="application/json")
public Emp insret(@RequestBody Emp em){
//do ....
return er.save(em);
}