【问题标题】:how to post json data to restService by using AngularJS如何使用AngularJS将json数据发布到restService
【发布时间】: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


    【解决方案1】:

    乍一看像

    <div ng-controller="item">
    

    应该是

    <div ng-controller="ItemsCtrl">
    

    【讨论】:

      猜你喜欢
      • 2018-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-06
      • 2016-09-01
      • 2018-12-01
      • 2014-07-23
      • 1970-01-01
      相关资源
      最近更新 更多