【发布时间】:2015-01-29 11:46:37
【问题描述】:
我正在使用 Spring MVC 框架并在首页中使用 AngularJS。我需要将 JSON 数据发布到 restservice。从主文件进行测试和编码的服务一切正常。但问题是当我尝试使用 Angular Js 发布数据时它失败了。您能否检查我的 js 和 jsp 文件,其中列出了数据并告诉我哪里出错了。我期待您的帮助。
JS 文件:
var helloAjaxApp = angular.module("helloAjaxApp", []);
helloAjaxApp.controller("ItemsCtrl", ['$scope', '$http', function($scope, $http) {
$scope.item = [
{ 'name':'burger',
'price': 1.99},
];
$scope.addRowAsyncAsJSON = function(){
$scope.companies.push({ 'name':$scope.name, 'price': $scope.price});
// Writing it to the server
//
var dataObj = {
name : $scope.name,
price : $scope.price
};
var res = $http.post('/item', dataObj);
res.success(function(data, status, headers, config) {
alert('going to other method 5' );
$scope.message = data;
});
alert('going to other method 4' );
res.error(function(data, status, headers, config) {
alert( "failure message: " + JSON.stringify({data: data}));
});
$scope.name='';
$scope.price='';
};
}]);
列出数据结果的JSP:
<div ng-controller="item">
<p>The ID is {{item}}</p>
<p>The content is {{item.id}}</p>
</div>
【问题讨论】:
标签: angularjs rest spring-mvc