简体   繁体   中英

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. 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?

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. 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. There are also other parameters that are returned like the original request sent from 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.

One of:

text: uses XHR.responseText.

xml: uses XHR.responseXML.

json: uses XHR.responseText parsed as JSON.

arraybuffer: uses XHR.response.

blob: uses XHR.response.

document: uses XHR.response.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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