Cordova 应用程序通过模拟器连接互联网,但在设备上没有连接 [更新:ajax 问题]

Cordova app has internet connection via emulator, but not on device [update: ajax problem]

我正在尝试通过 cordova 构建一个小型应用程序,将数据发送到服务器上的 PERL 脚本。调试和发布构建在 genymotion 模拟器上工作正常,但是从 Android 手机发送数据不起作用。该应用程序没有错误消息(也没有,应该在连接失败时显示)。 运行 USB 调试,我确实收到以下调用错误消息(savedataLastpage 函数应该发送数据):

Uncaught TypeError: Illegal invocation
at e (jquery-2.1.3.min.js:4)
at Ac (jquery-2.1.3.min.js:4)
at Function.n.param (jquery-2.1.3.min.js:4)
at Function.ajax (jquery-2.1.3.min.js:4)
at Object.saveDataLastPage (index.js:631)
at Object.renderLastPage (index.js:461)
at Object.recordResponse (index.js:597)
at HTMLButtonElement.<anonymous> (index.js:357)
at HTMLButtonElement.dispatch (jquery-2.1.3.min.js:3)
at HTMLButtonElement.r.handle (jquery-2.1.3.min.js:3)

相关代码如下:

index.js:631

saveDataLastPage:function() {
 $.ajax({
        type: 'post',
        url: '/URL/',
        data: localStore,
        crossDomain: true,
        success: function (result) {
        var pid = localStore.participant_id, snoozed = localStore.snoozed, uniqueKey = localStore.uniqueKey, pause_time=localStore.pause_time;
        localStore.clear();
        localStore.participant_id = pid;
        localStore.snoozed = snoozed;
            localStore.uniqueKey = uniqueKey;
            localStore.pause_time=pause_time;
        $("#question").html("<h3>Thank you, your responses have been sent.</h3>");
        },

        error: function (request, error) {
            console.log(error);
             $("#question").html("<h3>Error: Please check your internet connection.</h3><br><button>Send again</button>");
             $("#question button").click(function () {app.saveDataLastPage();});    
            }
        });

},

index.js:461

else {
    var datestamp = new Date();
    var year = datestamp.getFullYear(), month = datestamp.getMonth(), day=datestamp.getDate(), hours=datestamp.getHours(), minutes=datestamp.getMinutes(), seconds=datestamp.getSeconds(), milliseconds=datestamp.getMilliseconds();
    localStore[uniqueKey + '.' + "completed" + "_" + "completedSurvey"  + "_" + year + "_" + month + "_" + day + "_" + hours + "_" + minutes + "_" + seconds  + "_" + milliseconds] = 1;    
    app.saveDataLastPage();
}

如前所述,在 genymotion 模拟器上,ajax 脚本工作正常,没有错误并将数据发送到脚本。

我不确定为什么模拟器可以正常工作,但错误 Uncaught TypeError: Illegal invocation 表明这是 ajax post 调用的问题。具体来说,将数据处理成查询字符串的默认设置可能会失败。

From the ajax documentation:

By default, data passed in to the data option as an object (technically, anything other than a string) will be processed and transformed into a query string, fitting to the default content-type "application/x-www-form-urlencoded". If you want to send a DOMDocument, or other non-processed data, set this option to false.

这意味着您可以关闭 processData: false 处理 post 请求正文中的数据,或者您必须将 localStore 数据转换为适当的对象(从之前的任何内容)。

如果它像一个对象,您可以执行以下操作:

var data = {}; for (var elem in localStore) { data[elem] = localStore[elem]; }

或者可能简而言之:

var data = {}; localStore.each(function(elem) { data[elem.name] = elem.value; });