0

Angular js code

$scope.removeItemFromCart = function (productId) {

$http.put("removeItem/"+productId).success(function() {
    alert("Item Removed");
    $scope.refreshCartItems();
}).error(function(){
    alert("Error");
    $scope.refreshCartItems();
});

};

Rest controller

 @RequestMapping(value = "/removeItem/{productId}", method = RequestMethod.PUT)
   @ResponseStatus(value = HttpStatus.NO_CONTENT)
    public void removeItem (@PathVariable("productId") int productId) {
        System.out.println("Deleting item Id: "+productId);
        CartItem cartItem = cartItemService.getCartItemByItemId(productId);
        cartItemService.removeCartItem(cartItem);

    }

Following is my code for removing an item from my cart. the problem is when i click on remove button in my cart it removes the item but it doesnt enter the success function in my $http.put().success rather show me the error message in the error function of my $http.put().error these functions are in my angular controller. CAn someone tell me what is the problem and where is my error?

  • Can you tell me the error code and error message that server is returning? – Sachin Gupta Oct 04 '16 at 06:57
  • i only get a javascript alert "error" which i have written in the angular js code – NAiVE_developer Oct 04 '16 at 07:11
  • in error function you can pass `data, status, header, config` parameters. You can find out error message and other info from these parameters.Or if you can use browser's network tab to get info of rest call – Sachin Gupta Oct 04 '16 at 07:15

1 Answers1

0

I recommend this

  var req = {
     method: 'PUT',
     url: $consts.base_url + 'removeItem/',
     data: productId,
  };

  req.headers = {
     "content-type":"application/x-www-form-urlencoded", 
     "accept" : "application/json",
     "Authorization" : none
  }

  $http(req).error(function(){
     alert("Error");
  $scope.refreshCartItems();
  }).success(function() {
     alert("Item Removed");
     $scope.refreshCartItems();
  });

If you got error, please let me know the error types in inspect.

Zhao.YM
  • 61
  • 1
  • 1
  • 11