Search code examples
javascriptxmlhttprequestopenshift-originswagger-play2

Why I get readystate undefined and status 0 if I call same rest Api multiple times on openshift origin?


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)
            }

Solution

  • 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