简体   繁体   English

从控制器返回不完整的JSON数据-AngularJS

[英]Returning incomplete JSON data from controller - AngularJS

I am new to AngularJS and trying to work on a Single Page Cart functionality using AngularJS 1.x. 我是AngularJS的新手,并尝试使用AngularJS 1.x来处理单页购物车功能。 I have a JSON data from where I am showing my product list. 我在显示产品列表的地方有一个JSON数据。 The view is below: 该视图如下:

在此处输入图片说明

When I am clicking the 'More Info' button it is rediredting to a product details page. 当我单击“更多信息”按钮时,它会重新显示到产品详细信息页面。 While doing so I am encountering an error: 这样做时遇到错误:

SyntaxError: Unexpected end of JSON input SyntaxError:JSON输入意外结束

The reason behind it is the JSON data. 其背后的原因是JSON数据。 The JSON data that is retrieved during the call is not a complete one, hence the error. 调用期间检索到的JSON数据不完整,因此会出错。 Logged JSON data is below: 记录的JSON数据如下:

{"p_id":"1","p_name":"cotton tshirt","p_variation":"solid green","p_style":"ms13kt1906","p_selected_color":{"name":"blue","hexcode":" Thus I am getting the below view: {“ p_id”:“ 1”,“ p_name”:“棉质T恤”,“ p_variation”:“纯绿色”,“ p_style”:“ ms13kt1906”,“ p_selected_color”:{“ name”:“ blue”,“ hexcode “:”因此,我得到以下视图:

在此处输入图片说明

My controller code is: 我的控制器代码是:

angular.module('myApp.product.controllers', []).
controller('ProductsController',
['$scope', '$log', '$http', '$state',
function ($scope, $log, $http, $state) {

    $log.log('inside product controller');
    $http.get('data/cart.json').
    success(function (data) {
        $scope.products = data.productsInCart;
    }).
    error(function () {
        $log.log('could not find cart.json');
    });    
}]).
controller('ProductDetailsController',
['$scope', '$log', '$stateParams',
function ($scope, $log, $stateParams) {

    $log.log('inside product details controller ' + $stateParams.product);
    $scope.eachData = JSON.parse($stateParams.product);

}]);

The 'Partial' code is: “部分”代码为:

<div class="container main">
    <div class="row">
        <div class="col-md-4">
            <img ng-src="img/{{eachData.p_image}}" class="img-responsive" alt="product image">
        </div>
        <div class="col-md-8">
            <div class="thumbnail">
                <div class="caption-full">
                    <h4 class="pull-right">{{eachData.p_originalprice}}</h4>
                    <h4><a href="#">{{eachData.p_name}}</a></h4>
                    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum</p>
                    <p class="text-right">
                        <a href="#" class="btn btn-primary">Buy Now!</a>
                    </p>
                </div>
            </div>
        </div>
    </div>
</div>

My module code is: 我的模块代码是:

angular.module('cartApp.product').config([
    '$locationProvider', '$stateProvider',
    function($locationProvider, $stateProvider){
        $stateProvider.state('products', {
            url: '/products',
            controller: 'ProductsController',
            templateUrl: 'module/products/views/products.html'
        });
        $stateProvider.state('productsDetails', {
            url: '/details/:product',
            controller: 'ProductDetailsController',
            templateUrl: 'module/products/views/details.html'
        });
    }
]);

In my url I am getting the entire JSON data, but when I am trying to retrieve it and use it for my view page, the JSON data returned is incomplete, hence the error. 在我的网址中,我获取了整个JSON数据,但是当我尝试检索它并将其用于我的视图页面时,返回的JSON数据不完整,因此出现了错误。

Please help me understand the problem why the returned JSON data is incomplete when I am using it in my controller. 请帮助我理解在控制器中使用返回的JSON数据时为什么不完整的问题。

Guide me to a solution. 指导我解决。

Thank you in advance. 先感谢您。

Thanks to @BrianGlaz I was able to solve the problem. 感谢@BrianGlaz,我得以解决问题。 My JSON had # values for hexcode. 我的JSON具有十六进制代码的#个值。 That was causing the break. 那是造成休息的原因。 Now its working fine after removing the # from all the hexcode. 现在,从所有十六进制代码中删除#后,它的工作正常。

Thanks to all for taking time out to read and respond to my query. 感谢大家抽出宝贵的时间阅读并回复我的查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM