最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Polymer Iron Ajax - How to access Response from Request after Error Event? - Stack Overflow

programmeradmin16浏览0评论

I use iron-ajax:

<iron-ajax
   id="postLoginForm"
   method="POST"
   verbose
   url="../../login"
   content-type="application/json"
   handle-as="json"
   on-response="_handleLoginResponse"
   on-error="_handleErrorResponse"></iron-ajax>

The server always responds with an error if the request body is empty:

Error: The request failed with status code: 422

This triggers my _handleErrorResponse method in which I would like to access the actual response, which looks like this:

{"email":["The email field is required."],"password":["The password field is required."]}

Here is what my _handleErrorResponse looks like:

_handleErrorResponse: function (event) {
  console.log(event);
  console.log(event.detail);
  console.log(event.detail.error);
  console.log(event.detail.error.message);
  console.log(event.detail.request);
  console.log(event.detail.response);
  console.log(event.detail.request.response);
},

And here is what the output looks like:

So, how do I access the response so that I can output it to the view?

I use iron-ajax:

<iron-ajax
   id="postLoginForm"
   method="POST"
   verbose
   url="../../login"
   content-type="application/json"
   handle-as="json"
   on-response="_handleLoginResponse"
   on-error="_handleErrorResponse"></iron-ajax>

The server always responds with an error if the request body is empty:

Error: The request failed with status code: 422

This triggers my _handleErrorResponse method in which I would like to access the actual response, which looks like this:

{"email":["The email field is required."],"password":["The password field is required."]}

Here is what my _handleErrorResponse looks like:

_handleErrorResponse: function (event) {
  console.log(event);
  console.log(event.detail);
  console.log(event.detail.error);
  console.log(event.detail.error.message);
  console.log(event.detail.request);
  console.log(event.detail.response);
  console.log(event.detail.request.response);
},

And here is what the output looks like:

So, how do I access the response so that I can output it to the view?

Share Improve this question asked Feb 5, 2016 at 9:11 LoveAndHappinessLoveAndHappiness 10.1k22 gold badges74 silver badges107 bronze badges 2
  • 1 event.detail.request.xhr.response – Zabi Commented Feb 5, 2016 at 9:22
  • 1 Thank you... If you wish to formulate it to an answer, instead of a comment I can mark this question as answered. – LoveAndHappiness Commented Feb 5, 2016 at 9:28
Add a comment  | 

1 Answer 1

Reset to default 16

I think you can get the error JSON here:

event.detail.request.xhr.response

To get a more complete explanation, you can read the accepted answer to a different, but related question here:

Iron Ajax - How to access Response from on-response function?

Cheers!

发布评论

评论列表(0)

  1. 暂无评论