我在使用Android平台登录时遇到问题。以下代码与我的cordova phonegap build一起用于登录。苹果设备工作正常,登录没有任何问题。 Android设备都在第二次尝试时登录。
Android设备:
我第一次使用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数据,填充了应用程序,然后更改为正确的页面。
Apple设备:
苹果设备启动应用程序,我成功尝试,它读取我从服务器返回的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;
}
非常感谢任何帮助。
答案 0 :(得分:4)
async: false
问题是什么。 Android不允许你进行超过10秒的同步调用,并且无法更改此超时。试试async:true
。