Reputation: 4008
I'm using ajax to send data to backend.
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. 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.')
}
};
Upvotes: 0
Views: 27
Reputation: 621
Your logic is wrong in the onreadystatechange
callback, it should be
else if(xhr.readyState === 4 && xhr.status !== 200)
Upvotes: 1