簡體   English   中英

如何觸發Ajax( <iron-ajax > )叫聚合物?

[英]How to trigger Ajax ( <iron-ajax > ) call in polymer?

我只想在一些javascript函數中觸發ajax

我的代碼在這里:

<template is="dom-bind" id="app"> 
<iron-ajax auto url="{{ path('polymer_get_file') }}" handle-as="json" last-response="{{ '{{attachmentResponse}}' }}">
</iron-ajax>
  <vaadin-grid items="{{attachmentResponse.results' }}">
    <table>
      <colgroup>
        <col name="index">
        <col name="file"/>
        <col sortable name="file"/>
        <col name="type"/> 
      </colgroup> 
    </table>
  </vaadin-grid></template><script> function onClick (){
   // Here i need to trigger iron-ajax } </script>

您可以調用<iron-ajax>generateRequest()

<template is="dom-bind" id="app">
  <button on-tap="onClick">Send</button>
  <iron-ajax id="ajax" ...></iron-ajax>
</template>

<script>
  var app = document.getElementById('app');
  app.onClick = function() {
    this.$.ajax.generateRequest();
  };
</script>

 HTMLImports.whenReady(() => { const app = document.getElementById('app'); app.onClick = function() { this.response = null; this.$.ajax.generateRequest(); }; app.json = function(obj) { return JSON.stringify(obj, null, 2); }; }); 
 <head> <base href="https://polygit.org/polymer+1.11.3/components/"> <script src="webcomponentsjs/webcomponents-lite.js"></script> <link rel="import" href="polymer/polymer.html"> <link rel="import" href="iron-ajax/iron-ajax.html"> </head> <body> <template is="dom-bind" id="app"> <button on-tap="onClick">Send</button> <iron-ajax id="ajax" url="//httpbin.org/get" last-response="{{response}}"></iron-ajax> <pre>[[json(response)]]</pre> </template> </body> 

codepen

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM