2016-02-23 83 views
0

我正在jquery mobile和phonegapp中使用用户身份验证。首先,我将用户名和密码POST到服务器,如果成功,它会返回存储在本地存储中的一些数据。显示存储在本地存储到另一页的数据

这里$阿贾克斯后

$.ajax({ 
    type: 'POST', 
    dataType: "json", 
    url: KPSCtuts.Settings.Url, 
    data:"username=" + userName + "&password=" + password + "&login=", 
    success: function (resp) { 

     $.mobile.loading("hide"); 

     if (resp.success === true) { 

      // Create session. 
      var today = new Date(); 
      var expirationDate = new Date(); 
      expirationDate.setTime(today.getTime() + KPSCtuts.Settings.sessionTimeoutInMSec); 
      KPSCtuts.Session.getInstance().set({ 
       userProfileModel: resp.userProfileModel, 
       userId: resp.userId, 
       userName: resp.userName, 
       sessionId: resp.sessionId, 
       expirationDate: expirationDate, 
       keepSignedIn:me.$chkKeepSignedIn.is(":checked") 
      }); 
      // Go to main menu. 
      $.mobile.navigate(me.mainMenuPageId); 
      return; 
     } else { 
      if (resp.extras.msg) { 


         me.$ctnErr.html("<p>"+resp.extras.msg+"</p>"); 
         me.$ctnErr.addClass("bi-ctn-err").slideDown(); 

      } 
     } 
    }, 
    error: function (e) { 
     $.mobile.loading("hide"); 
     console.log(e.message); 
     // TODO: Use a friendlier error message below. 
     me.$ctnErr.html("<p>1-Oops! KPSCtuts had a problem and could not log you on. Please try again in a few minutes.</p>"); 
     me.$ctnErr.addClass("bi-ctn-err").slideDown(); 
    } 
}); 

这里是设置透过JSON.stringify的localStorage的数据session.js。

var KPSCtuts = KPSCtuts || {}; 
KPSCtuts.Session = (function() { 
var instance; 

function init() { 

    var sessionIdKey = "KPSCtuts-session"; 

    return { 
     // Public methods and variables. 
     set: function (sessionData) { 
      window.localStorage.setItem(sessionIdKey, JSON.stringify(sessionData)); 
     }, 

     get: function() { 

      var result = null; 

      try { 
       result = JSON.parse(window.localStorage.getItem(sessionIdKey)); 
      } catch(e){} 

      return result; 
     } 
    }; 
}; 

return { 
    getInstance: function() { 
     if (!instance) { 
      instance = init(); 
     } 
     return instance; 
    } 
}; 
}()); 

这里的localStorage的

KPSCtuts-session:{"userProfileModel":"Abhilash","userId":"1","userName":"abhilashrajrs","sessionId":"usr_b0424c8b16","expirationDate":"2016-03-23T19:16:16.319Z","keepSignedIn":true} 

我想要的用户名称显示另一个页面,我怎么能在localStorage的

回答

1

我拉了数据上的O/P认为它应该工作。

var userName = JSON.parse(localStorage.getItem('KPSCtuts-session'))['userName'];

在这里,你可以使用变量名。

+0

完美@Harin Kommuri – Raj

+0

我还有一个疑问可以帮助我。 有没有办法将服务器日期和时间拉到jquerymobile电话应用程序 – Raj

+0

Sry。我不知道。 – Harish

相关问题