Polymer Iron Ajax - How do I access a response to a request after an error event?

I am using 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 answers an error if the request body is empty:

Error: request failed with status code: 422

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

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

This 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 the result:

Developer withdrawal

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

+5
source share
1 answer

I think you can get the JSON error here:

 event.detail.request.xhr.response 

Hooray!

+12
source

Source: https://habr.com/ru/post/1242360/


All Articles