2017-04-05 60 views
-1

我創建了一個eshop,當我在Eclipse本地運行項目時,下面附加的腳本在用戶單擊按鈕立即訂購時執行。在此之後,購物車商品至少有1件商品。本地和服務器運行之間的意外不匹配

我上傳了Azure項目和按鈕的動作立即訂購無法正常工作。

viewProduct.jsp

<!-- Import spring text --> 
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form"%> 
<%@taglib prefix="spring" uri="http://www.springframework.org/tags"%> 
<%@include file="/WEB-INF/views/template/header.jsp"%> 


<div class="container-wrapper"> 
    <div class="container"> 
     <div class="page-header"> 
      <h1>Product Detail</h1> 

      <p class="lead">Here is the detail information of the product</p> 
     </div> 

     <!-- Grid system from Bootstrap --> 
     <div class="container" ng-app="cartApp"> 
      <div class="row"> 
       <div class="col-md-5"> 
        <img 
         src="<c:url value="/resources/images/${product.productId}.png" /> " 
         alt="image" style="width: 100%" /> 
       </div> 
       <div class="col-md-5"> 
        <h3>${product.productName}</h3> 
        <p>${product.productDescription}</p> 
        <p> 
         <strong>Manufacture</strong> : ${product.productManufacture} 
        </p> 
        <p> 
         <strong>Category</strong> : ${product.productCategory} 
        </p> 
        <p> 
         <strong>Condition</strong> : ${product.productCondition} 
        </p> 
        <p>${product.productPrice}USD</p> 

        <br> 

        <c:set var="role" scope="page" value="${param.role}" /> 
        <c:set var="url" scope="page" value="/product/productList" /> 
        <!-- if the user is admin we change the back page for the following "back" button --> 
        <c:if test="${role='admin'}"> 
         <c:set var="url" scope="page" value="/admin/productInventory" /> 
        </c:if> 

        <p ng-controller="cartCtrl"> 
         <a href="<c:url value="${url}" />" class="btn btn-default">Back</a> 
         <!-- we use the angular funtction (#) --> 
         <a href="#" class="btn btn-warning btn-large" 
          ng-click="addToCart('${product.productId}')"><span 
          class="glyphicon glyphicon-shopping-cart"></span>Order Now</a> 

         <a href="<spring:url value="/customer/cart" />" class="btn btn-default"><span 
          class="glyphicon glyphicon-hand-right"></span>View Cart</a> 
        </p> 
       </div> 
      </div> 
     </div> 

    </div> 
</div> 

<script src="<c:url value="/resources/js/controller.js" /> "></script> 
<%@include file="/WEB-INF/views/template/footer.jsp"%> 

在從controller.js cart.jsp文件我也嘗試執行clearCart()。代碼也不運行。

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> 
<%@include file="/WEB-INF/views/template/header.jsp" %> 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js"></script> 
<script src="<c:url value="/resources/js/controller.js" /> "></script> 

<div class="container-wrapper"> 
    <div class="container"> 
     <section> 
      <div class="jumbotron"> 
       <div class="container"> 
        <h1>Cart</h1> 

        <p>All the selected products in your shopping cart</p> 
       </div> 
      </div> 
     </section> 

     <section class="container" ng-app="cartApp"> 
      <div ng-controller = "cartCtrl" ng-init="initCartId('${cartId}')"> 
      <div> 
       <a class="btn btn-danger pull-left" ng-click="clearCart()"><span 
         class="glyphicon glyphicon-remove-sign"></span>Clear Cart</a> 
       <a href="<spring:url value="/order/${cartId}"/>" 
        class="btn btn-success pull-right"><span class="glyphicon-shopping-cart glyphicon"></span> Check out 
       </a> 
      </div> 

      <table class="table table-hover"> 
       <tr> 
        <th>Product</th> 
        <th>Unit Price</th> 
        <th>Quantity</th> 
        <th>Price</th> 
        <th>Action</th> 
       </tr> 
       <tr ng-repeat = "item in cart.cartItems"> 
        <td>{{item.product.productName}}</td> 
        <td>{{item.product.productPrice}}</td> 
        <td>{{item.quantity}}</td> 
        <td>{{item.totalPrice}}</td> 
        <td><a href="#" class="label label-danger" ng-click="removeFromCart(item.product.productId)"> 
         <span class="glyphicon glyphicon-remove"></span>remove</a></td> 
       </tr> 
       <tr> 
        <th></th> 
        <th></th> 
        <th>Grand Total</th> 
        <th>{{calGrandTotal()}}</th> 
        <th></th> 
       </tr> 
      </table> 

      <a href="<spring:url value="/" />" class="btn btn-default">Continue Shopping</a> 
      </div> 
     </section> 

    </div> 
</div> 


<%@include file="/WEB-INF/views/template/footer.jsp" %> 

controller.js

var cartApp = angular.module ("cartApp", []); 

cartApp.controller("cartCtrl", function ($scope, $http){ 

    $scope.refreshCart = function() { 
     $http.get('/eMusicStore/rest/cart/'+$scope.cartId).success(function (data) { 
      $scope.cart=data; 
     }); 
    }; 

    $scope.clearCart = function() { 
     $http.delete('/eMusicStore/rest/cart/'+$scope.cartId).success($scope.refreshCart()); 
    }; 

    $scope.initCartId = function (cartId) { 
     $scope.cartId = cartId; 
     $scope.refreshCart(cartId); 
    }; 

    $scope.addToCart = function (productId) { 
     $http.put('/eMusicStore/rest/cart/add/'+productId).success(function() { 
      alert("Product successfully added to the cart!") 
     }); 
    }; 

    $scope.removeFromCart = function (productId) { 
     $http.put('/eMusicStore/rest/cart/remove/'+productId).success(function (data) { 
      $scope.refreshCart(); 
     }); 
    }; 

    $scope.calGrandTotal = function() { 
     var grandTotal=0; 

     for (var i=0; i<$scope.cart.cartItems.length; i++) { 
      grandTotal+=$scope.cart.cartItems[i].totalPrice; 
     } 

     return grandTotal; 
    }; 
}); 
+0

_「不能正常工作」_是什麼意思? **如何**完全不起作用?你會得到任何錯誤消息(在服務器日誌中,在瀏覽器控制檯中)? –

+0

@JozefChocholacek沒有錯誤。我點擊** Order Now **,Angular代碼不會執行,因爲我在** addToCart **函數中的警報沒有出現。所以我認爲角碼永遠不會執行。 –

+0

你可以在** addToCart **函數的第一行添加一個'alert'來檢查它是否被調用? –

回答

0

請嘗試以下代碼登錄HTTP錯誤響應。

$http.put('/eMusicStore/rest/cart/remove/'+productId).success(function (data) { 
    $scope.refreshCart(); 
}).error(function (data, status, header, config) { 
    console.log(data);  
}); 
+0

所有這些代碼都來自電子課程。但我認爲存在一些問題。我可以看到,在initCartId中,refreshCart有一個參數(cartId),但在refreshCart的定義中沒有參數!在此之後,函數calGrandTotal沒有參數。那麼這個函數如何試圖找到grandTotal的結果呢?我想問一下,如果我說的話不適用,但我在Angular中真的很少。 –

+0

經過多次嘗試(並且在購物車開始創建時返回一些項目),當前的錯誤是。 ** SEVERE:與path [/ eMusicStore]上下文中的servlet [SpringMVC]引發Servlet.service()異常[Handler processing failed;嵌套異常是java.lang.StackOverflowError],其根本原因是 java.lang.StackOverflowError ** 我在一週前提出了這個錯誤。有人給了我一個鏈接,但我找不到發生了什麼問題。 –

相關問題