javascriptajaxsubmitpolymerresponsetext

How can i get responseText with Polymer is-ajax submit?


After submited "ajax-form", I can not get the responseText from java HttpServlet to Javascript.

HTML Code:

<form is="ajax-form" action ="URL" id="formID" method="POST" enctype="multipart/form-data">
 ....
</form>

JavaScript Code:

this.$.upload.submit();

Servlet Code:

response.getWriter().append("responseText ");

Solution

  • According to the doc, the XMLHttpRequest object is available in the submitted event:

    http://ajax-form.raynicholus.com/components/ajax-form/#ajax-form.events.submitted

    So you could handle that something like this:

    handleResponse: function(event) {
      this.response = event.detail.responseText;
      // do whatever you need to do with it.
    }
    

    If you're using this inside a Polymer element, you can use declarative event mapping like this:

    <form is="ajax-form" on-submitted="{{handleResponse}} action="URL" 
        id="formID" method="POST" enctype="multipart/form-data" >
     ...
    </form>
    

    Or you can add the listener imperatively:

    this.$.formID.addEventListener('submitted', handleResponse);
    

    Hope this helps.