Book Image

Spring MVC Beginner's Guide

By : Amuthan Ganeshan
Book Image

Spring MVC Beginner's Guide

By: Amuthan Ganeshan

Overview of this book

Table of Contents (19 chapters)
Spring MVC Beginner's Guide
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Time for action – consuming REST web services via Ajax


In order to consume Rest web services via Ajax, perform the following steps:

  1. Add a JavaScript file named controllers.js under the directory /src/main/webapp/resources/js/; then, add the following code snippets to it and save it:

    var cartApp = angular.module('cartApp', []);
    
    cartApp.controller('cartCtrl',  function ($scope, $http) {
      
      $scope.refreshCart = function(cartId) {
        $http.get('/webstore/rest/cart/'+$scope.cartId)
        .success(function(data) {
          $scope.cart = data;
        });
      };
      
      $scope.clearCart = function() {
        $http.delete('/webstore/rest/cart/'+$scope.cartId)
        .success($scope.refreshCart($scope.cartId));
        
      };
      
      $scope.initCartId = function(cartId) {
        $scope.cartId=cartId;
        $scope.refreshCart($scope.cartId);
      };
      
      $scope.addToCart = function(productId) {
        $http.put('/webstore/rest/cart/add/'+productId)
        .success(function(data) {
          $scope.refreshCart($http.get('/webstore/rest/cart...