2017-07-18 173 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不关心骆驼案例