how to post json data to restService by using angularjs

132 views
Skip to first unread message

Alper Kopuz

unread,
Dec 1, 2014, 8:05:04 AM12/1/14
to ang...@googlegroups.com

I am using Spring MVC framework and using AngularJs in the front page. I need to get post JSON Data to restservice. Everything works fine with the service that tests and coding from main file is fine. But the problem is when I try to post data by using Angular Js it fails. Could you please check my js and jsp file where data is listed and tell me where I make mistake . I am looking forward for your helps.

Regards Alper Kopuz


JS File :

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 where data result is listed:

    <div ng-controller="item">
        <p>The ID is {{item}}</p>
        <p>The content is {{item.id}}</p>
    </div>

Maximilian Noppel

unread,
Dec 19, 2014, 10:42:00 AM12/19/14
to ang...@googlegroups.com
Hey,

are you sure want to  post { 'name':$scope.name, 'price': $scope.price}? You defined $scope.item = [ ... ] and not $scope.name = / $scope.price = ? Did take a look at the requests your browser makes?

Greez,
Max
Reply all
Reply to author
Forward
0 new messages