polymerpolymer-2.xiron-ajax

Polymer iron-ajax POST method not working


I'm learning Polymer. I'm unable to figure the code to "post" using <iron-ajax>. I'm using an online testing API (https://reqres.in/), and I should receive this response back with status code 200:

{"token": "QpwL5tke4Pnpja7X"}".

I couldn't find a tutorial to show a POST example. I have been searching online for the past 24 hours, but everything is about GET and not POST.

If anyone familiar with <iron-ajax> could review my code and help me get it to work or figure out how to write the correct code, it would be very helpful for a beginner like me.

  1. Is my code correct with the body property?
  2. Is the response the 200 status code or the token?

    <!--
    @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-element.html">
    <link rel="import" href="shared-styles.html">
    <link rel="import" href="../bower_components/polymer/polymer.html">
    <link rel="import" href="../bower_components/iron-ajax/iron-ajax.html">
    
    <dom-module id="my-view2">
      <!--test using this data: {
          "email": "peter@klaven",
          "password": "cityslicka"
      }-->
      <template>
        <iron-ajax>
            auto 
            method="post"
            url="https://reqres.in/api/login"
            handle-as="json"
            content-type="application/json"
            body =[{"email": "peter@klaven", "password": "cityslicka"}]
            on-response={{handleResponse}}
    
        </iron-ajax>
    
        <!--Handle response-->
        <p> response handling code goes here, how to show the response from the server here?</p>
        <p> It should show: {"token": "QpwL5tke4Pnpja7X"} </p>
        <div>
        <p> {{handleResponse}} </p>
        </div>
      </template>
    
      <script>
        class MyView2 extends Polymer.Element {
          static get is() { return 'my-view2'}; 
    
          static get proporties() {
            return {
              handleResponse: {
                type: Object,
                notify: true,
                readOnly: true 
              }
            };
          }
        }
    
        window.customElements.define(MyView2.is, MyView2);
      </script>
    </dom-module>
    

Solution

  • The full example should look something like this:

    <dom-module id="x-foo">
      <template>
        <iron-ajax
                  auto
                  method="post"
                  url="//httpbin.org/post"
                  body='[{"foo": "{{foo}}"}]'
                  handle-as="json"
                  content-type="application/json"
                  last-response="{{lastResponse}}"
                  >
        </iron-ajax>
        <pre>[[json(lastResponse)]]</pre>
      </template>
      <script>
        class XFoo extends Polymer.Element {
          static get is() { return 'x-foo'; }
    
          static get properties() {
            return {
              foo: {
                type: String,
                value: 'bar'
              }
            }
          }
    
          json(obj) {
            return JSON.stringify(obj, null, 2);
          }
        }
        customElements.define(XFoo.is, XFoo);
      </script>
    </dom-module>
    

    demo