1

我在使用 android 平台登录时遇到问题。以下代码与我的 cordova phonegap 构建一起使用以登录。苹果设备工作正常,登录没有任何问题。安卓设备都在第二次尝试时登录。

安卓设备:

我第一次使用我的 android 设备登录时,出现以下错误:

Synchronous request timed out after 10 seconds for the 0th try, URL: 
Synchronous request timed out after 10 seconds for the 1th try, URL: 
Synchronous request timed out after 10 seconds for the 2th try, URL: 

在 android 上的第二次尝试我得到了成功的尝试,它读取我从服务器返回的 xml 数据,填充应用程序,然后更改到正确的页面。

苹果设备:

苹果设备启动应用程序,我成功尝试,它读取我从服务器返回的 xml 数据,填充应用程序,然后更改到正确的页面。

function login(){
    'use strict';
    var user = document.loginForm.username.value;
    var pwd = document.loginForm.password.value;
    var db = window.openDatabase("MobileDB", "3.0", "MobileDBData", 1000000);
    var loginSuccess = 0;
    console.log("setting user cookies with user name and password: " + user + ", " + pwd);

        // test for ipod/iphone/ipad
        var isiDevice = /ipad|iphone|ipod/i.test(navigator.userAgent.toLowerCase());
        // test for android device
        var isAndroid = /android/i.test(navigator.userAgent.toLowerCase());
        // retrieve the cookie from login

        if (isiDevice)
        {
            $.cookie('username', user);
            $.cookie('password', pwd);
        }
        else if (isAndroid)
        {
            window.localStorage.setItem('username', user);
            window.localStorage.setItem('password', pwd);
        }

        loginSuccess = ReadXML();
        console.error("Login Success after read XML " + loginSuccess);



}

我得到了正确的用户名和密码,但无论对于 android 设备,它需要 2 次登录才能工作。

function ReadXML() {
    'use strict';

    console.log("begin read xml");
    var url, isiDevice, isAndroid,userId,password,returnStatus;
    url = 'a url';

    // test for ipod/iphone/ipad
    isiDevice = /ipad|iphone|ipod/i.test(navigator.userAgent.toLowerCase());
    // test for android device
    isAndroid = /android/i.test(navigator.userAgent.toLowerCase());

    // retrieve the cookie from login
    if (isiDevice) {
        console.log("found an ipod, iphone, or ipad device");
        userId = $.cookie('username');
        password = $.cookie('password');
    } else if (isAndroid) {
        console.log("found an android device");
        userId = window.localStorage.getItem("username");
        console.log(userId);
        password = window.localStorage.getItem("password");
        console.log(password);
    }

    returnStatus = 0;
    console.log("reading xml with username and password: " + userId + ", " + password);
    var authorizationToken = "Basic " + userId + ":" + password;
    $(document).ready(function () {
                      $.ajax({
                             type: "POST",
                             beforeSend: function (request) {
                             request.setRequestHeader("AUTHORIZATION", authorizationToken);
                             },
                             async: false,
                             url: url,
                             dataType: "xml",
                             success: function (xml) {
                             returnStatus = 1;
                 console.error("Its Working");
                // do stuff 
                             },
                             error: function (x, status, error) {
                             returnStatus = 0;
                             console.error("Its broken");
                // do stuff

                           }
                        });
                      });
    return returnStatus;
}

任何帮助将不胜感激。

4

1 回答 1

4

async: false是问题。Android 不允许您进行耗时超过 10 秒的同步调用,并且无法更改此超时。尝试使用async:true.

于 2013-08-02T07:28:40.883 回答