我将尝试在Android上执行以下函数,以与我正在制作的物联网设备同步:
该设备需要一些时间来生成对请求的响应(约为45-50秒),因此我需要超时时间略长于30秒。如果我在iOS上将超时设置为5秒,它会执行此操作;如果我将其设置为90秒,则会等待整个时间。但是,在Android上,似乎忽略了这个参数。
我尝试在
我尝试使用小写的字母 "L",但仍然没有成功。
如何在 Cordova Android 应用程序中正确增加 AJAX 请求的超时参数?
更新: 我已更改函数,并确实在 30 秒后获得“已超时!!!”作为响应:
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);
}
}
complete()
方法来触发回调函数吗?同时设置一个错误函数,以确保超时是 $ajax 的真正问题。可以使用error()
方法中的error: function(x, t, m) {if(t==="timeout") {alert("got timeout"); } else { alert(t); } }
。 - Sim1-81