问题
I am having issues with my login with android platforms. The following code is used with my cordova phonegap build to log on. The apple device's work fine and log in without any issues. The android devices all log in on the second attempt.
Android device:
The first time I log in with my android devices i get the following error:
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:
On the second attempt on the android I get a successful attempt, it reads the xml data that I get back from the server, populates the app and then changes to the correct page.
Apple Device:
the apple device fires up the app, I get a successful attempt, it reads the xml data that I get back from the server, populates the app and then changes to the correct page.
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);
}
I get the correct usernames and passwords, but no matter what for the android device it takes 2 logins to work.
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;
}
Any help would be greatly appreciated.
回答1:
async: false Is the problem. Android doesnt let you make a synchronous call which takes more than 10 s and there s no way to change this timeout. Try with async:true.
来源:https://stackoverflow.com/questions/18001182/login-working-for-iphone-but-not-android