我正在学习 AngularJS
和 Spring MVC
。我陷入了这样一种情况:我有一个 angularjs Controller ,我可以从中对 spring 服务进行 REST API
调用。我想知道如何在 Controller 内接受这些 JSON 值并映射到模型(getter 和 setter)。
在数据库中进行插入之前,我正在 Spring 端进行一些验证。我还想知道如何将这些错误消息返回到 angula JS 并将它们填充到屏幕上。
这是我的 AngularJS Controller :
var myapp = angular.module("app");
myapp.controller("RedisplayController",function($scope,$localStorage,$http, $location,$window,) {
$scope.addconfig = function() {
var dataobj = {
escfirstname : $scope.escfirstname ,esclastname:$scope.esclastname,escage:$scope.escage
}
$http({
url: "http://localhost:8080/services/AddConfig",
method: "POST",
data: dataobj,
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
});
}
});
这是我的 Spring 服务:
@Controller
public class TestController {
@RequestMapping(value = "/AddConfig", method = RequestMethod.POST)
public @ResponseBody String PostService(@RequestBody Config person) {
System.out.println("came insidee");
return null;
}
}
我能够打印系统输出。现在我想知道如何继续。
最佳答案
像这样。
var myapp = angular.module("app");
myapp.controller("RedisplayController",function($scope,$localStorage,$http, $location,$window,) {
$scope.addconfig = function() {
var dataObject = {
escfirstname : $scope.escfirstname,
esclastname : $scope.esclastname,
escage : $scope.escage
};
$http({
method: 'POST',
url: 'http://localhost/jsonURL',
data: dataObject,
headers: {'Content-Type': 'application/json; charset=utf-8'}
}).success(function(data, status, headers, config) {
if( data ) {
}
else {
}
}).error(function(data, status, headers, config) {
console.log(status);
});
}
});
@Controller
public class TestController {
@RequestMapping(value = "/AddConfig", method = RequestMethod.POST)
public @ResponseBody String PostService(@RequestBody Config person) {
// System.out.println("person" + person.);
return null;
}
}
关于angularjs - 将 Json 数据从 Angular JS 传递到 Spring REST API 并处理它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40836439/