Iron-ajax无法读取json文件

时间:2017-04-11 15:04:24

标签: json polymer polymer-1.0 iron-ajax

我在第一个视图中使用了使用Polymer Starter Kit初始化的Polymer应用程序。我想在<paper-listbox>中读取一个JSON文件,并为每个数组元素创建一个<paper-item>

以下是我<paper-listbox>的代码:

<paper-listbox>
    <iron-ajax auto url="/listActivities.json" handle-as="json" last-response="{{activities}}" id="ajaxLoader"></iron-ajax>
    <template is="dom-repeat" items="{{activities}}">
        <paper-icon-item>
            <iron-icon icon="star" item-icon></iron-icon>
            <paper-item-body two-line>
              <div><span>{{item.label}}</span></div>
              <div secondary>Unit game number : <span>{{item.unitGames.length}}</span></div>
            </paper-item-body>
        </paper-icon-item>
    </template>
  </paper-listbox>

这是我的JSON文件:

[
{
    "_id":"58dbcb30dc0b2a01553c59a9",
    "staticMedia":"58d52d626575de2207193fac",
    "label":"myfirstgame",
    "__v":0,
    "unitGames":["58dbcaa5dc0b2a01553c59a8"]
},
{
    "_id":"58dd1d3edc0b2a01553c59af",
    "staticMedia":"58d12091241b0868bfb29a3b",
    "label":"Jeu à l'IUT",
    "__v":0,
    "unitGames":["58dd1b61dc0b2a01553c59ab"]
},
{
    "_id":"58dd23e14704644b4ab307e6",
    "staticMedia":"58d12091241b0868bfb29a3b",
    "label":"IUT",
    "__v":0,
    "unitGames":["58dd23ba4704644b4ab307e4"]
},
{
    "_id":"58e008d67d08713351f0af13",
    "staticMedia":"58d52d626575de2207193fac",
    "label":"Test POI clue",
    "__v":0,
    "unitGames":["58e008b97d08713351f0af12"]
},
{
    "_id":"58e10d2d7d08713351f0af22",
    "label":"Prunus MLG",
    "__v":0,
    "unitGames":["58e10d167d08713351f0af21"]
}
]

该应用不需要我的JSON文件的每个部分(只需要labelunitGames)。当我运行应用程序时,它在视图标题后没有显示任何内容。我该如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

您可能缺少该组件所需的一个或多个HTML导入:

<link rel="import" href="../iron-ajax/iron-ajax.html">
<link rel="import" href="../iron-icon/iron-icon.html">
<link rel="import" href="../iron-icons/iron-icons.html">
<link rel="import" href="../paper-listbox/paper-listbox.html">
<link rel="import" href="../paper-item/paper-item.html">
<link rel="import" href="../paper-item/paper-icon-item.html">
<link rel="import" href="../paper-item/paper-item-body.html">

使用上面的导入和listActivities.jsonplunker demo生成:

screenshot

答案 1 :(得分:0)

问题来自我的JSON文件的url路径,&#34; /&#34;之前不需要文件的名称,因为该文件与my-view1.html位于同一文件夹中。