无法在 Polymer 中使用 iron-ajax 响应处理程序检索 access_token

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

我在我的 Polymer 项目中使用 iron-ajax 来登录用户。将用户详细信息发送到数据库后,如果登录成功,我需要 return 一个令牌。但是,我在回复时遇到了问题。如果有人可以查看我的代码并告诉我我缺少什么,那就太好了。

当前,当我输出回购 属性 时,控制台 return 未定义。下面是我的 iron-ajax 我的属性的代码,以及当用户发送详细信息时调用的函数。 此外,我添加了我正在尝试的 return。

<dom-module id="login-form">
  <template>    
    <iron-ajax
      id="requestUser"
      url="http://api.dev/oauth/token"
      handle-as="json"
      method="POST"
      content-type="application/json"
      body='{
        "grant_type": "password",
        "client_id": 2,
        "client_secret": "KyHacYa2Q7DCTVLaDe1RSnvTlTI20CrmYb7FXujb",
        "username": "user1@test.com",
        "password": "1111",
        "scope": "*"}'
      last-response="handleResponse"></iron-ajax>

    </template>
    <script>

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

      //On-tap loginTheUser runs
      loginTheUser: function() {
        this.$.requestUser.generateRequest();
      },
      handleResponse: function (data) {
        this.repos = data.details.response;
        console.log(this.repos);
      },

   </script>
</dom-module>

以下是我尝试获取的令牌 returned:

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

<iron-ajax>.lastResponse is actually an Object that represents the last received response from the AJAX request. You could bind 到标记中的 属性(看起来 repos 是目标):

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

您正在传递一个方法名称,这意味着您实际上是在尝试为 <iron-ajax>.response event. To do that, you could create an annotated event listener 设置一个处理程序:

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

请注意,您的响应处理程序错误地尝试读取 data.details,而它应该是 data.detail(其中 dataCustomEvent)。