0

I get the readystate undefined and status 0 as response when I call the same API multiple times and call it quickly one after another. I have buttons on my webpage for calling api and if I give gap in clicking e.g. 3 seconds, the api works fine. I checked the backend openAPI (swagger) rest service which runs fine even if I call it quickly.

So I tried to do some testing with readystate and interestingly while the output of readystate is "undefined", the if condition on readystate == 4 works and give the output as "undefined" and status "0". See the last else if condition.

But the main question is why I get undefined while the backend is working fine? and the api also works fine if called with delay? Adding delay is hard because end-user can click buttons on screen, plus I also don't want unnecessary delay.

function getDocking(url,callBackFunction,btnId){
                var xmlhttp = new XMLHttpRequest();
                var response;
                xmlhttp.onreadystatechange = function() 
                {

                    if (this.readyState == 4 && this.status == 200) 
                    {
                        //Calling the callback function displayDocking once got response from server
                        callBackFunction(this,btnId);
                    }else if(this.readyState == 4 && this.status == 400) 
                        {
                            document.getElementById('warning').style.display = "block";
                            document.getElementById("warning").innerHTML =  this.responseText;
                        }
                        else if(this.readyState == 4)
                        {
                            alert("Received for button " + btnId + " Readystate: " + this.readystate + " Status: " + this.status)
                            callBackFunction(this,btnId);
                        }
                };

                xmlhttp.open( "POST", url , true );
                xmlhttp.send(null);
                //alert("Calling docking for Button " + btnId + " with URL " + url)
            }
user1859366
  • 113
  • 1
  • 9
  • 1
    JavaScript is case sensitive. In the `alert(...)` line you need to use `this.readyState` instead of `this.readystate`. – Helen Apr 15 '19 at 07:43
  • Possible duplicate of [XMLHttpRequest ReadyState always undefined](https://stackoverflow.com/questions/13636612/xmlhttprequest-readystate-always-undefined) – Helen Apr 15 '19 at 07:44
  • 1
    @Helen Your comments helped me. Yes I was getting readyState = 4, so I started to search on why status is 0. I am using openshift origin and there was a request timeout limit on routes. I increased it and it solved the problem. Thanks. – user1859366 Apr 15 '19 at 21:12

1 Answers1

1

I am using openshift origin and a limit on routes was causing request timeout. I edited the yaml file and increased it to 60s. Defautl value is 30s.

haproxy.router.openshift.io/timeout: 60s 
user1859366
  • 113
  • 1
  • 9