简体   繁体   English

失败时的Ajax响应

[英]Ajax response in case of failing

I'm using ajax to send data to backend. 我正在使用ajax将数据发送到后端。

My issue is that until the result is returning from the server I get the fail message, and after that Success Message. 我的问题是,直到结果从服务器返回之前,我都会收到失败消息以及成功消息之后。

I presume that it happens, because it goes to else until a response is received. 我相信它会发生,因为它关系到else ,直到收到响应。 How can I avoid this issue. 我如何避免这个问题。

xhr.onreadystatechange = function () {
                if (xhr.readyState === 4 && xhr.status === 200) {
                    setResponse(JSON.parse(xhr.responseText).message);
                }
                else if(xhr.status !== 200) {
                    setResponseMessage('Form Failed.Please contact support.')
                }
            };

您的逻辑在onreadystatechange回调中是错误的,应该是

else if(xhr.readyState === 4 && xhr.status !== 200)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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