[英]Polymer - Iron Ajax - How to Handle Error Response
I have a standard iron-ajax
element that sends a login
request with an email
and password
to the backend. 我有一个标准的iron-ajax
元素,该元素将带有email
和password
的login
请求email
到后端。
<iron-ajax
id="postLoginForm"
method="POST"
verbose
url="../../login"
content-type="application/json"
handle-as="json"
last-error="{{lastError}}"
last-response="{{lastResponse}}"
on-response="_handleLoginResponse"></iron-ajax>
My _handleLoginResponse
is this, which works fine btw: 我的_handleLoginResponse
是这样,顺便说一句:
_handleLoginResponse: function (event) {
console.log(event.detail.response);
},
This is always the response I am getting from the server: 这始终是我从服务器获得的响应:
{"email":["The email field is required."],"password":["The password field is required."]}
The problem is now, that the _handleLoginResponse
method is only called, when the status code of the response is 200
. 现在的问题是, 仅当响应的状态代码为200
时, 才调用_handleLoginResponse
方法。
But if the login failes, the servers sends a status code of 422 Unprocessable Entity
that does not trigger the _handleLoginResponse
method. 但是,如果登录失败,服务器将发送状态代码 422 Unprocessable Entity
不可422 Unprocessable Entity
,该状态代码不会触发_handleLoginResponse
方法。 Though the response contains the above mentioned json
string. 尽管响应中包含上述json
字符串。
I would like to send the request without credentials. 我想发送没有凭据的请求。 If the request successively fails I receive a response with a status code that is not 200. I want to display the errors on the page. 如果请求连续失败,我将收到状态码不是200的响应。我想在页面上显示错误。
But I don't know how to access the response, if the _handleLoginResponse
method does not trigger, when the status code is not 200. 但是我不知道如何访问响应,如果状态代码不是200时_handleLoginResponse
方法未触发。
How do I trigger the _handleLoginResponse
method, when I get a 422
status code? 当我收到422
状态代码时,如何触发_handleLoginResponse
方法?
All 4** and 5** are error codes. 所有4 **和5 **均为错误代码。 This means you might also have to listen to iron-ajax error
event. 这意味着您可能还必须收听Iron-ajax error
事件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.