javascript - 如果我在 openshift 源上多次调用相同的 rest Api,为什么我会得到 readystate undefined 和 status 0?
问题描述
当我多次调用同一个 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)
}
解决方案
我正在使用 openshift origin,并且路由限制导致请求超时。我编辑了yaml文件并将其增加到60s。默认值为 30 秒。
haproxy.router.openshift.io/timeout: 60s
推荐阅读
- python-3.x - 如何自动获取json数据而不是手动复制粘贴?
- json - 如何将本地 JSON 读入 Angular 打字稿文件
- laravel - 如何在一个域 NGINX 中设置两个应用程序?
- imagej - 是否有内置功能来查找操作系统?
- android - Android Studio 无法生成包或 APK
- java - 通过flask将图像文件从spring boot java发送到python
- javascript - 带有 Bootstrap 5 的 Vue.js (2.x)
- c# - 向 MongoDB 中插入多个数据时避免重复
- swift - 如何使用 func 快速将数字转换为单词
- json - 如何在 json 提取器中查找 json 数组长度