简体   繁体   English

聚合物-铁Ajax-如何处理错误响应

[英]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元素,该元素将带有emailpasswordlogin请求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字符串。

What I would like to do: 我想做的是:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM