How to get a response from a Polymer 1 request with an iron form

I am trying to get an answer from iron-form in Polymer 1 .

The form send a php call to a script that returns the HTML code to insert into the div ( ul and some li ).

I am using the iron-form event " iron-form-response ", but I do not know how to get the answer.

I see the answer on the web tab of the browser developer tools, but I don’t know how to get it in my element.

I did not find how to do this in the iron-form documentation on the Internet .

Can anybody help me?

+8
javascript polymer
source share
5 answers

What's going on guys? All of these answers confuse the OP when it is just like this:

Your form:

 <form is="iron-form" on-iron-form-response="responseHandler" action="http://localhost" id="myform"> <!-- Your form elements --> </form> 

Your script:

 <script> Polymer({ // Some scripts here. // ...now your listener responseHandler: function(e) { console.log(e.detail.response); }, }); </script> 

Just. Nothing complicated. Do not overdo it.

+8
source share

Add hearing listeners to the iron form.

 ready: function(){ this.$.myform.addEventListener('iron-form-response',this.formResponse); this.$.myform.addEventListener('iron-form-error',this.formError); } 

Form Response Function:

 formResponse: function (e){ console.log("Server Response: ",e.detail); } 

Form Error Function:

 formError: function (e){ console.log("Form Error: ",e.detail); } 
+3
source share

I'm going to build Talon's answer, which is correct.

e.detail will be a JSON object, assuming that the response sent from the server is in JSON form. So, if you use Node.JS and Express, you might get this code:

 document.getElementById('my-form').addEventListener('iron-form-response', function (e) { console.log('Form :', e.detail); }); 

And your server code might look like this:

 res.status(200).json({'foo': 'bar'}); 

Then e.detail will be the object {"foo": "bar"}

0
source share

A small update. I am sending json with the answer: res.contentType('json'); es.status(500).send({"foo":"bar"}); res.contentType('json'); es.status(500).send({"foo":"bar"}); If I use 500 (error), I can get json data only console.log(e.detail.request.xhr.response); In the case of code 200, the following is achieved: console.log(e.detail.response); Do not understand why this is so, but this is the only way for me ((

0
source share
  <script> Polymer({ is: 'rsvp-wedding', attached: function() { var form = document.querySelector('form'); form.addEventListener('iron-form-error', function(e) { console.log(e.detail.request.status); }); } }); </script> 
0
source share

All Articles