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.
body
property?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>
Your HTML is malformed (perhaps a copy-paste typo?). The iron-ajax
's attributes should be inside the opening tag like this:
<iron-ajax
auto
method="post"
...
>
</iron-ajax>
You probably meant to bind the handleResponse
property to <iron-ajax>.lastResponse
, which contains the response to the AJAX request.
<iron-ajax last-response={{handleResponse}} ...>
Note that the binding of <p>{{handleResponse}}</p>
would render the response object as [object Object]
. If you want to see the response contents, you'll have to use a computed binding that returns a string (e.g., with JSON.stringify()
) like this:
// <template>
<p>json(handleResponse)</p>
// <script>
class XFoo extends Polymer.Element {
...
json(obj) {
return JSON.stringify(obj);
}
}
The attribute value of <iron-ajax>.body
should be single-quoted like this:
<iron-ajax body='[{"foo": "bar"}]'>
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>