如何读取角度js中的JSON文件

时间:2016-01-24 12:32:27

标签: javascript angularjs json

我正在开展一个项目,我必须阅读该产品并将其显示在我的HTML页面上 这是我的控制器

  app.controller('SecondCtrl', function($scope,srvShareCart ,srvShareData,srvAddtoCart,$http) {
      $scope.product=[];
    $scope.One=function(){
     //alert($scope.ids);
       $scope.ids= srvAddtoCart.getData();
       for (var i=0; i<$scope.ids.length; i++){
      $http.get("http://www.ilexsquare.com/JwelTech/wordpress/api.php?function=GetProduct&id="+$scope.ids[i])
        .then(function (response) {

            srvShareCart.addData(response.data);
        });

        $scope.cart = srvShareCart.getData();
        console.log($scope.cart);

     }

    }

这是我的HTML页面

<div ng-controller="SecondCtrl">
    <ul> <li class=oxy-list__item  ng-repeat="d in cart">{{d.title}}</li></ul></div>

这是一个有效的API

这是示例API link

它在 response.data 中给出了一个错误,它是未定义的。它的问题是什么? 我得到的输出,但它不是一个数组的形式..我如何在数组中添加这些产品。我试过$scope.carts.push($scope.products);但它没有存储为数组

2 个答案:

答案 0 :(得分:2)

从API发送的响应内容类型为then: thrown(RequestFormatException) ,而不是plain text

enter image description here

答案 1 :(得分:2)

试试这样。

         $http.get({
            url: "http://www.ilexsquare.com/JwelTech/wordpress/api.php?function=GetProduct&id="+$scope.ids[i],
            dataType: "json",
            success: function (response) {
                srvShareCart.addData(response.data);
            },
            error: function (msg) {
                alert(msg.d);
            }
        });