Nested Iron Ajax

253 views Asked by At

Ok. So my last post was too ambiguous. For my second post, let me try to approach the same problem in hopefully a little more straighforward manner. Below is the code. Here is a screenshot of the results I get. Regarding the second iron-ajax call, if I use curl in terminal with this () I get what I want (it's a link preview service, so title, img, desc etc). Trying to accomplish the same with iron-ajax post with required parameters defined per spec. I don't get any console errors (for the first time) and based on the [object.Object] result I get when I output the last-response variable in the body of second dom-repeat, appears to be returning a json object just like the first iron-ajax call (which does work, includes the link but not enough data about it, hence running link through second service that returns the data I want to display).

Result from running code locally

CODE:

<dom-module id="my-new-view">
<template>
<!-- Defines the element's style and local DOM -->
  <style>
  :host {
    display: block;

    padding: 16px;
  }
</style>
<iron-ajax auto
  url="https://api.rss2json.com/v1/api.json?rss_url=http://feeds.feedburner.com/DrudgeReportFeed"
  params="{"fmt":"xml-rss"}"
  handle-as="json"
  last-response="{{ajaxResponse}}"></iron-ajax>
<p>First: {{ajaxResponse}}</p>
<template is="dom-repeat" items="[[ajaxResponse.items]]" as="item" index-as="item_no">
<p>{{item.title}}</p>

<iron-ajax auto
method="post"  
url="https://guteurls.de/api/"
  params="{"u":"{{item.guid}}", "r":"https://127.0.0.1", "e":"s652imb8et42xd0bd", "t":"json"}"
  handle-as="json"
  last-response="{{newAjaxResponse}}"></iron-ajax>

  <p>Second: {{newAjaxResponse}}</p>


  <template is="dom-repeat" items="[[newAjaxResponse.newItems]]" as="newItem" index-as="newItem_no">

    <p>{{newItem.title}}</p>
    <paper-card heading="{{newItem.title}}" image="{{newItem.image.url}}" alt="{{newItem.title}}">
      <div class="card-content">
       <h1>Description: {{newItem.desc}}</h1>
       <p>Test</p>
      </div>
      <div class="card-actions">{{newItem.title}}
        <paper-button>Share</paper-button>
        <paper-button>Explore!</paper-button>
      </div>
    </paper-card>

</template>
</template>
</template>

<script>
class MyNewView extends Polymer.Element {
static get is() { return 'my-new-view'; }
}



customElements.define(MyNewView.is, MyNewView);


</script>
</dom-module>
1

There are 1 answers

5
Ofisora On

Problems and Solutions:

  1. params="{"fmt":"xml-rss"}"
    • Quoting not done properly. You can you single quote as well like
      params='{"fmt":"xml-rss"}' or params="{'fmt':'xml-rss'}"
  2. First: {{ajaxResponse}} and Second: {{newAjaxResponse}}

    • You can use console to debug since you cannot display object like that
  3. params="{"u":"{{item.guid}}", "r":"https://127.0.0.1", "e":"s652imb8et42xd0bd", "t":"json"}"

    • Quoting not done properly.
    • Attribute binding i.e. {{item.guid}} must be followed by $.
    • Change to params$='{"u":"{{item.guid}}", "r":"https://127.0.0.1", "e":"s652imb8et42xd0bd", "t":"json"}'
  4. newAjaxResponse.newItems
    • There is no newItems in newAjaxResponse. Just use newAjaxResponse
    • [Note: newAjaxResponse is returned as Object which must be converted to Array since dom-repeat works only with Array.]
  5. Before you define fields like desc, image.url make sure it exists.

Working code:

<dom-module id="my-new-view">
<template>
<!-- Defines the element's style and local DOM -->
  <style>
  :host {
    display: block;

    padding: 16px;
  }
</style>
<iron-ajax auto url="https://api.rss2json.com/v1/api.json?rss_url=http://feeds.feedburner.com/DrudgeReportFeed" params='{"fmt":"xml-rss"}' handle-as="json" last-response="{{ajaxResponse}}"></iron-ajax>
<p>First: {{ajaxResponse}}</p>
<template is="dom-repeat" items="[[ajaxResponse.items]]" as="item" index-as="item_no">
  <p>{{item.title}}</p>

  <iron-ajax auto method="post" url="https://guteurls.de/api/" params$='{"u":"{{item.guid}}", "r":"https://127.0.0.1", "e":"s652imb8et42xd0bd", "t":"json"}' handle-as="json" last-response="{{newAjaxResponse}}"></iron-ajax>

  <p>Second: {{newAjaxResponse}}</p>


  <template is="dom-repeat" items="[[_toArray(newAjaxResponse)]]" as="newItem" index-as="newItem_no">

   <paper-card heading="{{newItem.title}}" image="{{newItem.img}}" alt="{{newItem.title}}">
     <div class="card-content">
       <h1>Description: {{newItem.description}}</h1>
       <p>Test</p>
     </div>
     <div class="card-actions">{{newItem.title}}
      <paper-button>Share</paper-button>
      <paper-button>Explore!</paper-button>
     </div>
  </paper-card>

  </template>
</template>
</template>

<script>
class MyNewView extends Polymer.Element {
  static get is() { return 'my-new-view'; }

  _toArray(obj) {
      var tempArray = [];
      tempArray.push(obj);
      //console.log(tempArray);
      return tempArray;
    }

}



customElements.define(MyNewView.is, MyNewView); 

</script>
</dom-module>

You can check the working demo here.