2017-07-18 176 views
0

我正在測試聚合物的鐵ajax電話。爲此,我設置了一個虛擬視圖:聚合物鐵ajax不工作

<link rel="import" href="../bower_components/polymer/polymer-element.html"> 
<link rel="import" href="shared-styles.html"> 
<link rel="import" href="../bower_components/iron-flex-layout/iron-flex-layout-classes.html"> 

<dom-module id="my-voltage"> 
    <template is="auto-binding"> 
    <style include="shared-styles iron-flex iron-flex-alignment"> 
    .circle { 
    display: inline-block; 
    text-align:center; 
    color: black; 
    border-radius: 70%; 
    background: #ddd; 
    font-size: 30px; 
    width: 110px; 
    height: 110px; 
    margin: 15px; 
    } 
    </style> 
     <iron-ajax id="ajax" 
        auto 
        url="https://jsonplaceholder.typicode.com/posts/1" 
        last-response="{{data}}" 
        on-response="_onResponse" 
        handleAs="json"> 
     </iron-ajax> 
     <div class="circle">{{data.id}}</div> 
    </template> 
    <script> 
    class MyVoltage extends Polymer.Element { 
     static get is() { 
     return "my-voltage"; 
     } 

     _onResponse() { 
     setTimeout(() => this.$.ajax.generateRequest(), 500); 
     } 
    } 
    customElements.define(MyVoltage.is, MyVoltage); 
    </script> 
</dom-module> 

但是,這不起作用。我想每隔半秒鐘ping一次API,但它甚至不會加載一次:我只是變成一個空圈。 enter image description here

我在這裏錯過了什麼?爲什麼API調用不起作用?謝謝!

回答

0

那麼首先想到的是你忘了導入它?根據您共享的代碼,您只能導入聚合物元素,共享樣式和iron-flex-layout-classes。 我看到的下一件事是handleAs參數必須寫爲句柄 - 因爲html不關心駱駝案例