user3541631
user3541631

Reputation: 4008

Ajax response in case of failing

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

Answers (1)

Madhav Chaturvedi
Madhav Chaturvedi

Reputation: 621

Your logic is wrong in the onreadystatechange callback, it should be

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

Upvotes: 1

Related Questions