当我多次调用同一个 API 并一个接一个地快速调用它时,我得到了 readystate undefined 和 status 0 作为响应。我的网页上有用于调用 api 的按钮,如果我在点击例如 3 秒时给出间隙,则 api 工作正常。我检查了后端 openAPI (swagger) 休息服务,即使我快速调用它也能正常运行。
因此,我尝试使用 readystate 进行一些测试,有趣的是,当 readystate 的输出为“未定义”时,readystate == 4 上的 if 条件起作用并将输出为“未定义”和状态“0”。查看最后一个 else if 条件。
但主要问题是为什么我在后端工作正常时得到未定义?如果延迟调用,api也可以正常工作吗?添加延迟很困难,因为最终用户可以单击屏幕上的按钮,而且我也不想要不必要的延迟。
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)
}