Ich entwickle eine PhoneGap-Anwendung mit jQuery Mobile 1.3.1 & jQuery JavaScript Library v1.9.1. Ich habe ein Anmeldeformular mit einem Benutzernamen und einem Passwort. Es gibt eine JSON-API, die JSON-Daten zur Verarbeitung des Login senden und empfangen kann.
Ich benutze das folgende JavaScript für den Ajax-Login.
$('#submit').bind('click', function(e) {
e.preventDefault();
$.ajax({
type : "POST",
url : "http://domainx/public/login",
xhrFields : {withCredentials: true},
crossDomain: true,
beforeSend : function() {$.mobile.showPageLoadingMsg();},
complete : function() {$.mobile.hidePageLoadingMsg();},
data : {username : 'subin', password : 'passwordx'},
dataType : 'json',
success : function(response) {
console.error(JSON.stringify(response));
},
error : function() {
console.error("error");
}
});
});
Hier ist mein HTML-Code für das Login-Formular.
<div data-role="content">
<form id="login-form" data-ajax="false" method="post">
<fieldset>
<input type="text" name="username" id="username" value="subin" placeholder="Username">
<input type="password" name="password" id="password" value="passwordx" placeholder="Password">
<input type="button" data-theme="b" name="submit" id="submit" value="Enter" data-icon="plus">
</fieldset>
</form>
</div>
Aber das funktioniert einfach nicht. Der Server gibt einen Anmeldefehler zurück, obwohl er für andere Apps problemlos funktioniert.
Sie haben mehrere Fehler, ist Ihr Code:
Datum in Daten ändern
data : {username : 'subin', password : 'passwordx'},
Entfernen Sie diese Zeile, um zu verhindern, dass Phonegap erfolgreich gepostet wird:
xhrFields : {withCredentials: true},
Verwenden Sie diese Funktion in Ihrem beforeSend und complete, um den Loader anzuzeigen:
$.mobile.loading('show');
und
$.mobile.loading('hide');
ihre jetzigen sind in jQuery 1.2+ veraltet.
Ihr endgültiger Code sollte folgendermaßen aussehen:
$.ajax({
type : "POST",
url : "http://domain/public/login",
crossDomain: true,
beforeSend : function() {$.mobile.loading('show')},
complete : function() {$.mobile.loading('hide')},
data : {username : 'subin', password : 'passwordx'},
dataType : 'json',
success : function(response) {
//console.error(JSON.stringify(response));
alert('Works!');
},
error : function() {
//console.error("error");
alert('Now working!');
}
});
Beweisen Sie, dass es funktioniert. Führen Sie es einfach von der Festplatte und nicht von einem Server aus, da Sie einen CROSS-Domänenfehler erhalten:
<!DOCTYPE html>
<html>
<head>
<title>jQM Complex Demo</title>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>
<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; minimum-scale=1.0; user-scalable=no; target-densityDpi=device-dpi"/>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.css" />
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js"></script>
<script>
$.ajax({
type : "POST",
url : "http://domain/public/login",
crossDomain: true,
beforeSend : function() {$.mobile.loading('show')},
complete : function() {$.mobile.loading('hide')},
data : {username : 'subin', password : 'passwordx'},
dataType : 'json',
success : function(response) {
//console.error(JSON.stringify(response));
alert('Works!');
},
error : function() {
//console.error("error");
alert('Not working!');
}
});
</script>
</head>
<body>
<div data-role="page" id="index">
<div data-theme="b" data-role="header">
<h1>Index page</h1>
</div>
<div data-role="content">
</div>
</div>
</body>
</html>
Sie haben hier einen Typ:
date : {username : 'subin', password : 'passwordx'},
//--^^^^----this should be named data
versuchen Sie, dies zu ändern:
data : {username : 'subin', password : 'passwordx'},
verwenden Sie den Datentyp Jsonp
dataType : 'jsonp'
ermöglicht die domänenübergreifende