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

余生颓废 提交于 2019-12-21 03:59:31

问题


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?


回答1:


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!



来源:https://stackoverflow.com/questions/35220177/polymer-iron-ajax-how-to-access-response-from-request-after-error-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!