我试图在AngularJS中解析JSON数组。但这不起作用。如果该数组是直接写入的,则它可以正常工作。帮我解决问题。我一直在使用的代码如下。
(function () {
var app = angular.module('store', ['ngCookies']);
app.controller('StoreController', ['$scope', '$cookies', function ($scope, $cookies) {
$scope.products = JSON.parse(SuccessItems);
$scope.cart = [];
$scope.total = 0;
var expireDate = new Date();
expireDate.setDate(expireDate.getDate() + 1);
$cookies.putObject('cart', $scope.cart, { 'expires': expireDate });
$scope.cart = $cookies.getObject('cart');
$scope.total += parseFloat(product.price);
$cookies.put('total', $scope.total, { 'expires': expireDate });
};
}]);
var SuccessItems = '';
$.ajax({
type: "POST",
url: "code.aspx/RetrieveItems",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
success: function (jsondata) {
SuccessItems = jsondata.d.replace(/"(\w+)"\s*:/g, '$1:').replace(/"(\d[.]\d+)"/g, "$1");
},
error: function (xhr, ajaxOptions, thrownError) {
errMsg = JSON.parse(xhr.responseText).Message;
alert(baseErrorMsg + 'Error Code : ' + errMsg);
}
})
})();