【问题标题】:iron-ajax can not find generateRequest()-Methodiron-ajax 找不到 generateRequest()-方法
【发布时间】:2017-09-29 19:46:52
【问题描述】:

我是 Polymer 的新手,我尝试通过 Iron-ajax 创建一个 ajax 调用。我遇到了一个错误,generateRequest()未定义。

聚合物 1 正在使用中。

我用这个命令安装了 iron-ajax:bower install --save PolymerLabs/promise-polyfill#1

如你所见,我用<link rel="import" href="../bower_components/promise-polyfill/promise-polyfill-lite.html">加载它

我将它与一些示例进行比较,但找不到错误。 有人有想法或解决方案吗?

这是代码:

<!--
@license
Copyright (c) 2016 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->

<link rel="import" href="../bower_components/polymer/polymer.html">
<link rel="import" href="shared-styles.html">
<link rel="import" href="../bower_components/promise-polyfill/promise-polyfill-lite.html">
<dom-module id="my-view2">
  <template>
    <style include="shared-styles">
      :host {
        display: block;

        padding: 10px;
      }
    </style>

    <div class="card">
      <div class="circle">2</div>
      <h1>View Two</h1>
      <p>Demo output</p>
    </div>

    <template is="dom-repeat" items="[[repos]]">
      <span>{{items.name}}</span>
    </template>
    <button on-click="setajax">Click me</button>

    <iron-ajax
            id="ajax"
            url=""
            params='{"type":"all"}'
            handle-as="json"
            on-response="hresponse"
            debounce-duration="300" >
    </iron-ajax>
  </template>

  <script>
    Polymer({
      is: 'my-view2',
        setajax: function () {
            this.$.ajax.url = "https://api.github.com/users/burczu/repos";
            this.$.ajax.params= {"type":"all"};
            this.$.ajax.generateRequest();
      },
        hresponse: function(request) {
            console.log(request.detail.response);
            console.log(this.$.ajax.lastResponse);
        }
    });
  </script>
</dom-module>

【问题讨论】:

    标签: polymer iron-ajax


    【解决方案1】:

    你需要安装iron-ajax:

    bower install --save PolymerElements/iron-ajax
    

    然后,您可以使用以下代码导入:

    <link rel="import" href="../bower_components/iron-ajax/iron-ajax.html">
    

    完成上述操作后,您的代码就可以工作了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-04
      • 2015-12-16
      • 2018-11-21
      相关资源
      最近更新 更多