Niklas Lang Niklas Lang - 1 year ago 101
JSON Question

Unable to retrieve access_token with iron-ajax response handler in Polymer

I am using iron-ajax in my Polymer project to login a user. After sending the user details to the database I need to return a token if the login succeeds. However, I am having trouble with the response. It would be great If someone could look over my code and tell me what I am missing.

Currently the console returns me undefined when I output the repos property. Below is the code of my iron-ajax my properties and the function that gets called when a user sends the details.
In addition I added what I am trying to return.

<dom-module id="login-form">
"grant_type": "password",
"client_id": 2,
"client_secret": "KyHacYa2Q7DCTVLaDe1RSnvTlTI20CrmYb7FXujb",
"username": "",
"password": "1111",
"scope": "*"}'


is: 'login-form',
properties: {
repos: {
type: Array

//On-tap loginTheUser runs
loginTheUser: function() {
handleResponse: function (data) {
this.repos = data.details.response;


The following is the token i try to get returned:

"token_type": "Bearer",
"expires_in": 3155673599,
"access_token": "eyJ0eXAiOiJKV1...",
"refresh_token": "271VWcUXisybg="

Answer Source

<iron-ajax>.lastResponse is actually an Object that represents the last received response from the AJAX request. You could bind that to a property in your markup (it seems repos is the target):

<iron-ajax last-response="{{repos}}">

You were passing a method name, which implies you're actually trying to set a handler for the <iron-ajax>.response event. To do that, you could create an annotated event listener with:

<iron-ajax on-response="handleResponse">

Note that your response handler is incorrectly trying to read data.details when it should be data.detail (where data is a CustomEvent).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download