Lil*_*its 5 ajax jquery android cordova
我正在尝试在Android上执行以下功能以与我正在制造的IoT设备同步:
function NewDeviceFetchDeviceID(){
strURL = "https://192.168.x.x/.....";
return $.ajax({
url: strURL,
type: 'GET',
timeout: 90000
});
}
Run Code Online (Sandbox Code Playgroud)
设备需要一些时间来响应请求(大约45-50秒),因此我需要将超时时间略长于30秒。超时似乎在iOS上有效-如果我将其设置为5秒,它将完成,如果将其设置为90,它将等待整个时间。对于Android,似乎忽略了该参数。
我尝试添加以下到config.xml下<platform name="android">没有运气:
<preference name="LoadUrlTimeoutValue" value="90000"/>
Run Code Online (Sandbox Code Playgroud)
我尝试使用小写的“ L”,但还是没有运气:
<preference name="loadUrlTimeoutValue" value="90000"/>
Run Code Online (Sandbox Code Playgroud)
如何在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);
}
}
Run Code Online (Sandbox Code Playgroud)
因此,经过大约一个月的努力,我最终使用了Cordova Advanced HTTP插件:https://www.npmjs.com/package/cordova-plugin-advanced-http
使用该setRequestTimeout方法,我能够将 HTTP 请求超时增加到 120 秒:
cordova.plugin.http.setRequestTimeout(120.0);
Run Code Online (Sandbox Code Playgroud)
在重写我的使用此插件的请求后,我的问题在 Android 9/10 上得到了解决,并且在 iOS 12/13 上一切仍然按预期工作。
| 归档时间: |
|
| 查看次数: |
161 次 |
| 最近记录: |