最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to trigger a iron-ajax request on a paper-button click? - Stack Overflow

programmeradmin3浏览0评论

I would like <iron-ajax> to POST dynamic data from a <textarea> to when I click a <paper-button> element:

function get_data() {
  return {content:document.getElementById("mycontent").html()}
}
<html>
  <head>
    <!-- Imports-->
  </head>
  <body>
    <iron-ajax
      url="//example"
    ></iron-ajax>
    <paper-button id="mybutton"></paper-button>
    <textarea id="mycontent"></textarea>
  </body>
</html>

I would like <iron-ajax> to POST dynamic data from a <textarea> to http://example when I click a <paper-button> element:

function get_data() {
  return {content:document.getElementById("mycontent").html()}
}
<html>
  <head>
    <!-- Imports-->
  </head>
  <body>
    <iron-ajax
      url="//example"
    ></iron-ajax>
    <paper-button id="mybutton"></paper-button>
    <textarea id="mycontent"></textarea>
  </body>
</html>

How can I bine the iron-ajax and paper-button elements to send the data to the server?

Share Improve this question edited Oct 26, 2015 at 11:42 Sebastian Wozny asked Oct 26, 2015 at 11:36 Sebastian WoznySebastian Wozny 17.6k7 gold badges54 silver badges73 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 7

You need to wrap your polymer elements in a tag that will register as polymer element. You can use dom-bind in your case.

<template id="t" is="dom-bind">           
    <textarea value="{{dataToPost::input}}"></textarea>
    <paper-button on-tap="postData">Post Data</paper-button>
    <iron-ajax 
    id="dataAjax" 
    method="post"
    url="data/url"
    on-response="postComplete"></iron-ajax>
</template>  

In the script you need to call generateReqeust on iron-ajax element.

(function (document) {
'use strict';
document.addEventListener('WebComponentsReady', function() {

    // We have to bind the template with the model
    var t = document.querySelector('#t');
    var ajaxRequest = t.$.dataAjax;

    // make the iron-ajax call
    t.postData = function() {
      ajaxRequest.body = {
        'text': t.dataToPost;
      } 
      ajaxRequest.generateRequest();
    }

    //callback on request plete
    t.postComplete = function(){
      alert('whoa! request plete');
    }
});
})(document);

Working plunker for GET: http://plnkr.co/edit/13QJ7QFETIBg4bEiCMS7?p=preview

The simplest and least expensive way is to just query the iron ajax element and call the method generateRequest()

Place this in your on-tap handler for paper-button...

Polymer.dom(this.root).querySelector('iron-ajax').generateRequest()

If you are keen on not using a <template is="dom-bind"> (it would make it a lot easier though), you can go the plain vanilla route (i.e. no dom-bind).

<html>

<head>
  <!-- Imports-->
</head>

<body>
  <iron-ajax url="//example"></iron-ajax>
  <paper-button id="mybutton"></paper-button>
  <textarea id="mycontent"></textarea>
  <script>
    window.addEventListener('WebComponentsReady', function() {
      var ironAjax = document.querySelector('iron-ajax');
      ironAjax.method = 'post';
      ironAjax.contentType = 'text/plain'; // you're just sending text
      var myButton = document.getElementById('mybutton');
      var myContent = document.getElementById('mycontent');

      myButton.addEventListener('tap', function(e) {
        var valueToSend = myContent.value;
        ironAjax.body = valueToSend;
        ironAjax.generateRequest();
      });
    });
  </script>
</body>

</html>

发布评论

评论列表(0)

  1. 暂无评论