【发布时间】:2019-08-31 14:50:09
【问题描述】:
我正在尝试在 Android 上执行以下功能以与我正在制作的 IoT 设备同步:
function NewDeviceFetchDeviceID(){
strURL = "https://192.168.x.x/.....";
return $.ajax({
url: strURL,
type: 'GET',
timeout: 90000
});
}
设备需要一些时间来响应请求(大约 45-50 秒),因此我需要将超时时间略长于 30 秒。超时似乎适用于 iOS - 如果我将其设置为 5 秒,它会这样做,如果我将其设置为 90,它将一直等待。对于 Android,似乎忽略了这个论点。
我尝试将以下内容添加到 <platform name="android"> 下的 config.xml 中,但没有成功:
<preference name="LoadUrlTimeoutValue" value="90000"/>
我尝试使用小写“L”,但仍然没有运气:
<preference name="loadUrlTimeoutValue" value="90000"/>
如何正确增加 Cordova Android 应用中 AJAX 请求的超时参数?
更新: 我已经改变了功能,我确实得到了“超时!!!”作为恰好 30 秒后的响应:
function TryXHR(){
try{
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
alert("ready state = 4");
}
};
strURL = "https://192.168.x.x/......";
xhr.open("POST", strURL, true);
xhr.setRequestHeader("Content-type", "application/json; charset=utf-8");
xhr.timeout = 120000; // Set timeout to 120 seconds (120000 milliseconds)
xhr.onload = function () { alert(xhr.response); }
xhr.ontimeout = function () { alert("Timed out!!!"); }
xhr.onerror = function () { alert("Non-timeout error"); }
xhr.send();
}catch(err){
alert("exception caught: "+err.message);
}
}
【问题讨论】:
-
不幸的是,这是在 Java 中,我正在用 Cordova 中的 JavaScript 编写我的应用程序
-
你也检查过服务器吗?还有一种叫做服务器超时的东西。 Check this 如果请求有超时参数,请检查网络选项卡。希望有帮助:)
-
服务器端没有超时限制。我可以看到服务器仍在解密来自移动应用的传入信息,并且(Android)移动应用放弃了。
-
而不是
return $.ajax({.....你是否尝试使用$.ajaxcomplete()方法来触发你的回调?也尝试设置一个错误函数,以确保超时是 $ajaxerror()方法error: function(x, t, m) {if(t==="timeout") { alert("got timeout"); } else { alert(t); } }的真正问题
标签: android jquery ajax cordova