Polymer Iron Ajax - 如何在错误事件后访问请求的响应? [英] Polymer Iron Ajax - How to access Response from Request after Error Event?
问题描述
我使用iron-ajax:
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:
错误:请求失败,状态码为:422
Error: The request failed with status code: 422
此触发器我的 _handleErrorResponse
方法,我想访问实际的响应,如下所示:
This triggers my _handleErrorResponse
method in which I would like to access the actual response, which looks like this:
{email:[电子邮件字段是必需的。],密码:[密码字段是必需的。]}
{"email":["The email field is required."],"password":["The password field is required."]}
以下是我的 _handleErrorResponse
的样子:
_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);
},
以下是输出结果:
那么,如何访问响应以便我可以将其输出到查看?
So, how do I access the response so that I can output it to the view?
推荐答案
我认为你可以在这里得到错误JSON:
I think you can get the error JSON here:
event.detail.request.xhr.response
To得到更完整的解释,你可以在这里阅读一个不同但相关的问题的接受答案:
To get a more complete explanation, you can read the accepted answer to a different, but related question here:
干杯!
这篇关于Polymer Iron Ajax - 如何在错误事件后访问请求的响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!