javascript - Angularjs GET request ends with 400(). Incorrect formulated inquiry? -
i have webshop project in spring, i'm trying handle adding itemst cart. script fails on refreshcart method. gives
http status 400 - incorrect request, check request data
so here's angualarjs script:
var cartapp = angular.module('cartapp', []); cartapp.controller('cartctrl', function ($scope, $http) { $scope.refreshcart = function (cartid) { console.log("cart id in refreshcart method controllers.js " + cartid); $http({ method: 'get', url: '/rest/cart/' + cartid }).then(function successrefresh(singlecart) { $scope.cart = singlecart; console.log("success, im inside"); console.log(singlecart); console.log("after"); }, function errorcallback(singlecart) { console.log("error in refreshcart"); }); }; $scope.clearcart = function () { $http.delete('/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('/rest/cart/add/' + productid) .success(function (data) { $http({ method: 'get', url: '/cart/getcartid' }).then(function successcallback(response) { $scope.cartid = response.data; $scope.refreshcart($scope.cartid); }, function errorcallback(response) { }); alert("product has been succesfully added cart!"); }); }; $scope.removefromcart = function (productid) { $http.put('/rest/cart/remove/' + productid) .success(function (data) { $scope.refreshcart($http.get('/rest/cart/get/cartid')); }); }; });
spring rest controllers:
@controller @requestmapping(value = "rest/cart") public class cartrestcontroller { @autowired private cartservice cartservice; @autowired private productservice productservice; //////////////////////////// @requestmapping(value = "/{cartid}", method = requestmethod.get) public @responsebody cart read(@pathvariable(value = "cartid") string cartid) { system.out.println("i'm in read method in /rest/cart controller. cartid: " + cartid); (item item : cartservice.read(cartid).getproducts().values()) { system.out.println(item.getproduct().getproductname()); } return cartservice.read(cartid); } /** * post - creates new cart object * - sends cart object id = cartid * put - updates cart object id = cartid * delete - delete cart object id = cartid */ @requestmapping(method = requestmethod.post) public @responsebody cart create(@requestbody cart cart) { system.out.println("i'm in create method in /rest/cart controller. cartid: " + cart.getcartid()); return cartservice.create(cart); } ////////////////////////////////////////// @requestmapping(value = "/{cartid}", method = requestmethod.put) @responsestatus(value = httpstatus.no_content) public void update(@pathvariable(value = "cartid") string cartid, @requestbody cart cart) { system.out.println("i'm in update method in /rest/cart controller. cartid: " + cartid + " new cart : " + cart.getcartid()); cartservice.update(cart, cartid); } @requestmapping(value = "/{cartid}", method = requestmethod.delete) @responsestatus(value = httpstatus.no_content) public void delete(@pathvariable(value = "cartid") string cartid) { system.out.println("i'm in delete method in /rest/cart controller. cartid: " + cartid); cartservice.delete(cartid); } @requestmapping(value = "/add/{productid}", method = requestmethod.put) @responsestatus(value = httpstatus.no_content) public void additem(@pathvariable integer productid, httpservletrequest request) { system.out.println("i'm in additem method in /rest/cart controller. productid: " + productid); string sessionid = request.getsession().getid(); cart cart = cartservice.read(sessionid); if (cart == null) { cart = cartservice.create(new cart(sessionid)); } product product = productservice.findbyid(productid); if (product == null) { throw new illegalargumentexception(); } cart.additemtocart(new item(product)); cartservice.update(cart, sessionid); system.out.println("i'm in additem method in /rest/cart controller. cart values: "); (item item : cart.getproducts().values()) { system.out.println(item); } } @requestmapping(value = "/remove/{productid}", method = requestmethod.put) @responsestatus(value = httpstatus.no_content) public void removeitem(@pathvariable integer productid, httpservletrequest request) { system.out.println("i'm in removeitem method in /rest/cart controller. productid: " + productid); string sessionid = request.getsession().getid(); cart cart = cartservice.read(sessionid); if (cart == null) { cart = cartservice.create(new cart(sessionid)); } product product = productservice.findbyid(productid); if (product == null) { throw new illegalargumentexception(); } cart.removeitem(new item(product)); cartservice.update(cart, sessionid); } @exceptionhandler(illegalargumentexception.class) @responsestatus(value = httpstatus.bad_request, reason = "incorrect request, check request data") public void handleclienterrors(exception ex) { } @exceptionhandler(exception.class) @responsestatus(value = httpstatus.internal_server_error, reason = "internal server error") public void handleservererrors(exception ex) { } }
spring controllers:
@controller @requestmapping(value = "/cart") public class cartcontroller { @requestmapping(value = "/getcartid", method = requestmethod.get) public @responsebody string getcartid(httpservletrequest request) { system.out.println("i'm in getcartid method in /rest/cart controller. session id: " + request.getsession(true).getid()); return request.getsession().getid(); } @requestmapping(value = "/{cartid}", method = requestmethod.get) public string getcart(@pathvariable(value = "cartid") string cartid , modelmap model){ system.out.println("i'm in getcart method in /cart controller. cartid: " + cartid); model.addattribute("cartid", cartid); return "cart"; } @requestmapping public string get(httpservletrequest request){ system.out.println("i'm in method in /cart controller. session id: " + request.getsession(true).getid() ); return "redirect:/cart/" + request.getsession(true).getid(); } }
when want add new item cart addtocart function being invoked. according logs controllers/dao/services ok this, after want refresh cart. , there problem.
the request
$http({ method: 'get', url: '/rest/cart/' + cartid }).then(function successrefresh(singlecart) { $scope.cart = singlecart; console.log("success, im inside"); console.log(singlecart); console.log("after"); }, function errorcallback(singlecart) { console.log("error in refreshcart"); });
ends
http status 400 - incorrect request, check request data
so don't know items in cart, because dont see any...
i can paste jsp files if needed.
Comments
Post a Comment