![](/img/trans.png)
[英]How to redirect after successful login from a bootstrap modal page to a different view or a page with the login user information
[英]phonegap login page, how to keep user information
我有一個登錄頁面,我想存儲用戶信息。 如果user為null,則將用戶發送到登錄頁面,否則用戶將繼續使用該應用程序。
$.mobile.changePage("index.html");
我的代碼是:
var obj; //user information (json)
$(document).ready(function(e) {
$("#btnlogin").click(function(){
var username=$("#lusername").val();
var password=$("#lpassword").val();
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Service1.asmx/Giris",
data: "{'kullaniciadi':'"+ username+"' , 'sifre': '"+password+"'}",
dataType: "json",
success: function(msg) {
obj = jQuery.parseJSON( msg.d);
if(obj!=null)
{
$.mobile.changePage("index.html");
}
else alert("not found");
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
});
});
因為我正在使用$.mobile.changePage("index.html");
我可以在每個頁面上使用相同的Java腳本,我使用警報,我可以工作
$('#illnessDetailsPage').live('pageshow', function(event) {
alert(user);
});
我的問題從這里開始,我在javascript頂部使用var obj//user info
,但是當頁面更改時,它返回null
。
為什么它為空? (以更好地學習javascript)以及如何解決問題?
那么如何控制“后退”按鈕在登錄時不加載登錄頁面? 我沒有找到任何例子,提前謝謝。 (我不想使用localStorage)
每當您加載頁面時,資源都會再次加載。 這實質上意味着您存儲在javascript變量中的信息將丟失。
現在進入您的解決方案,因為您正在使用phonegap,因此您的應用程序將在基於Webkit的瀏覽器上運行。 因此,我建議您使用localStorage。
在登錄成功功能中,您可以設置userInfo。
$("#btnlogin").click(function(){
var username=$("#lusername").val();
var password=$("#lpassword").val();
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Service1.asmx/Giris",
data: "{'kullaniciadi':'"+ username+"' , 'sifre': '"+password+"'}",
dataType: "json",
success: function(msg) {
localStorage.setItem('userInfo', msg.d);
//Your code here
}
現在,在其他頁面中,您只需檢查此本地存儲對象即可。 如果為null,則將頁面更改為登錄頁面。
if(localStorage.getItem('userInfo') == null){
$.mobile.changePage('login.html');
}
我還建議您檢查indexedDb https://developer.mozilla.org/en/docs/IndexedDB
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.