PhoneGap登录页面,如何保存用户信息

phonegap login page, how to keep user information

本文关键字:保存 用户 信息 何保存 登录 PhoneGap      更新时间:2023-09-26

我有一个登录页面,我想存储用户信息。如果用户为空,则发送用户到登录页面,否则用户将继续使用应用程序。

$.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 脚本,所以我使用 alert 并且我工作

$('#illnessDetailsPage').live('pageshow', function(event) {
    alert(user);
});

我的问题从这里开始,我在javascript的顶部使用var obj//user info但是当页面更改时,它会返回null

为什么它是空的?(为了更好地学习JavaScript)以及如何解决问题?

那么如何控制"后退"按钮在登录时不加载登录页面呢?我没有找到任何例子, 提前谢谢你。(我不想使用本地存储)

每当加载页面时,都会再次加载资源。这实质上意味着您存储在javascript变量中的信息将丢失。

现在来到您的解决方案,因为您使用的是 phonegap,因此您的应用程序将在基于 webkit 的浏览器上运行。所以我建议你使用localStorage。

在登录成功功能中,您可以设置用户信息。

$("#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');
}

我还建议您查看索引数据库https://developer.mozilla.org/en/docs/IndexedDB