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

标签 javascript jquery cordova

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

$.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
    }

在其他页面中,您现在可以仅检查此本地存储对象。如果为空,则将页面更改为登录页面。

if(localStorage.getItem('userInfo') == null){
   $.mobile.changePage('login.html');
}

我还建议您查看indexedDb https://developer.mozilla.org/en/docs/IndexedDB

关于javascript - PhoneGap登录页面,如何保存用户信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19143763/

相关文章:

javascript - 如何从 JSON 格式的 Wikipedia OpenSearch API 访问数据?

javascript - 插入和查看唯一 ID 的有效方法

javascript - 将行添加到 HTML 表而不重新渲染的最佳方法

android - 为什么没有定义 Cordova ?

javascript - Angularjs ng-view 被禁止

javascript - 附加链接而不重定向

javascript - 从输入表单获取文本返回空字符串,但 console.log 显示显示文本

javascript - Phonegap Ajax 请求端口 8000

javascript - 将音频文件转换为base64

javascript - 如何在 Facebook 应用程序开发中使用外部 JavaScript 文件?