嵌套铁阿贾克斯

时间:2017-09-10 20:01:55

标签: javascript arrays polymer iron

确定。所以我的上一篇文章太模糊了。对于我的第二篇文章,让我试着以更直接的方式解决同样的问题。下面是代码。这是我得到的结果的屏幕截图。关于第二次铁-ajax调用,如果我在终端中使用curl(),我得到了我想要的东西(它是一个链接预览服务,所以title,img,desc等)。尝试使用iron-ajax post完成相同的操作,并根据规范定义所需的参数。我没有得到任何控制台错误(第一次)并且基于我在第二个dom-repeat体中输出last-response变量时得到的[object.Object]结果,似乎返回了json对象就像第一个iron-ajax调用(它确实有效,包括链接但没有足够的数据,因此通过第二个服务运行链接,返回我想要显示的数据)。

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 个答案:

答案 0 :(得分:0)

问题和解决方案:

  1. params="{"fmt":"xml-rss"}"
    • 报价未正确完成。你可以单引号,如同 params='{"fmt":"xml-rss"}'params="{'fmt':'xml-rss'}"
  2. First: {{ajaxResponse}}Second: {{newAjaxResponse}}

    • 您可以使用console进行调试,因为您无法显示
    • 这样的对象
  3. params="{"u":"{{item.guid}}", "r":"https://127.0.0.1", "e":"s652imb8et42xd0bd", "t":"json"}"

    • 报价未正确完成。
    • 属性绑定,即{{item.guid}}必须后跟$
    • 更改为params$='{"u":"{{item.guid}}", "r":"https://127.0.0.1", "e":"s652imb8et42xd0bd", "t":"json"}'
  4. newAjaxResponse.newItems
    • newItems中没有newAjaxResponse。只需使用newAjaxResponse
    • 即可
    • [注意:newAjaxResponse作为Object返回,必须转换为Array,因为dom-repeat仅适用于Array。]
  5. 在定义desc之类的字段之前,image.url确保它存在。
  6. 工作代码:

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

    您可以查看工作demo here

相关问题