【问题标题】:Cordova jQuery AJAX call timing out after 30 seconds regardless of timeout setting无论超时设置如何,Cordova jQuery AJAX 调用都会在 30 秒后超时
【发布时间】: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);
    }
}

【问题讨论】:

  • 我觉得这里有答案stackoverflow.com/questions/693997/…
  • 不幸的是,这是在 Java 中,我正在用 Cordova 中的 JavaScript 编写我的应用程序
  • 你也检查过服务器吗?还有一种叫做服务器超时的东西。 Check this 如果请求有超时参数,请检查网络选项卡。希望有帮助:)
  • 服务器端没有超时限制。我可以看到服务器仍在解密来自移动应用的传入信息,并且(Android)移动应用放弃了。
  • 而不是return $.ajax({..... 你是否尝试使用$.ajax complete() 方法来触发你的回调?也尝试设置一个错误函数,以确保超时是 $ajax error() 方法 error: function(x, t, m) {if(t==="timeout") { alert("got timeout"); } else { alert(t); } } 的真正问题

标签: android jquery ajax cordova


【解决方案1】:

因此,经过大约一个月的努力,我最终使用了 Cordova Advanced HTTP 插件:https://www.npmjs.com/package/cordova-plugin-advanced-http

使用setRequestTimeout 方法,我能够将HTTP 请求超时时间增加到120 秒:

cordova.plugin.http.setRequestTimeout(120.0);

在重写我使用此插件的请求后,我的问题在 Android 9/10 上得到了解决,在 iOS 12/13 上一切仍然正常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-19
    • 1970-01-01
    • 2018-03-11
    • 1970-01-01
    • 2013-10-06
    • 2016-06-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多