简体   繁体   English

在聚合物中是否可以将从铁质ajax获取的数据存储在阵列中?

[英]Is it possible in polymer to store the data you got from iron-ajax in an array?

in my way I ask for a REST-API and I get back a json-array. 以我的方式,我请求一个REST-API,然后返回一个json-array。 What can I do to store the data in an array? 我该怎么做才能将数据存储在数组中? Is it possible to push the data in an array by using the "responseHandler"-function by on-response? 是否可以通过on-response使用“ responseHandler”功能将数据推入数组中?

Here's my code: 这是我的代码:

<dom-module id="rest-api">
<template>      
    <iron-ajax 
        auto
        url="http://localhost:8080/cockpit/clients"
        handle-as="json"
        on-response="responseHandler" 
        last-response="{{response}}"
    ></iron-ajax>
        <table>
            <tr>
                <th>Client-ID</th>
                <th>Status</th>
            </tr>
            <template is="dom-repeat" items="{{response}}">
                <tr>
                    <td>{{item.id}}</td>
                    <td>{{item.status}}</td>
                </tr>
            </template>
        </table>
</template>
</dom-module>
<script>
  Polymer({
    is: 'rest-api',
    properties: {

    },
    responseHandler: function(e, request) {
        console.log("responseHandler fired!");
        // Can I do anything here?
    }
  });
</script>

Thanks for help! 感谢帮助!

Yes, you can. 是的你可以。 For this you need to add some code to your responseHandler func. 为此,您需要向responseHandler函数添加一些代码。 You get all your data in items and then can store it in place were you need: 您将所有数据存储在项目中,然后可以根据需要将其存储在适当的位置:

var items = e.detail.response;
for (var i=0; i<items.length; i++) {
//store your data
}

It depends what your response type is handled as but the typical use of json ends up going in event.detail.response. 这取决于您处理的响应类型,但是json的典型用法最终出现在event.detail.response中。 There are also other parameters that are returned like the original request sent from iron-request. 还有其他返回的参数,例如从iron-request发送的原始请求。 The event is the first parameter. 该事件是第一个参数。

From the polymer catalog on handleAs Specifies what data to store in the response property, and to deliver as event.detail.response in response events. 从handleA上的聚合物目录中指定要存储在响应属性中的数据,以及在响应事件中作为event.detail.response传递的数据

One of: 之一:

text: uses XHR.responseText. 文字:使用XHR.responseText。

xml: uses XHR.responseXML. xml:使用XHR.responseXML。

json: uses XHR.responseText parsed as JSON. json:使用XHR.responseText解析为JSON。

arraybuffer: uses XHR.response. arraybuffer:使用XHR.response。

blob: uses XHR.response. blob:使用XHR.response。

document: uses XHR.response. 文档:使用XHR.response。

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

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