如何将JSON数据数组从一个html页面传递到其他html页面?

时间:2015-03-03 12:05:14

标签: javascript php jquery html json

我正在为用户创建个人资料页面。我有两个html页面: page1:login.html page2:profile.html 。从一页:1我们登录,成功登录后,它将重定向到userProfile页面:2。我将第1页的用户信息发送到 第2页,如姓名,地址,手机号码,电子邮件ID等,以JSON格式,

我想使用sesstion,但不是localStorage

JSON格式为

[{"userInfo":{"id":"1","username":"nee","Name":"Neelabh", "address":"Bangalore","Email":"nee@gmail.com"}}]

我使用此参考作为帮助:JqueryMobile (JQM), Json and passing data to a new page

登录页面中的JavaScript:

 <script type="text/javascript">     
    $(document).ready(function(){       
                var userData = {
                 storeUserDataInSession: function(userData) {
                     var userObjectString = JSON.stringify(userData);                         
                window.sessionStorage.setItem('userObject',userObjectString)
                 },
                 getUserDataFromSession: function() {
                     var userData = window.sessionStorage.getItem('userObject')
                     return JSON.parse(userData);
                 }
                }   

               $("#register-form-header").submit(function(){            

                var uName = $('#userName').val();               
                var upassd=$('#password').val();            

                $.ajax({                                        
                    url:"http://localhost/login/login.php",                     
                    type:"POST",
                    dataType:"json",
                    data:{type:"login",uName:uName,password:upassd},                    

                    ContentType:"application/json",
                    success: function(response){    
                        $.each(response, function(index,value){                           


                        }); 
                        //alert(JSON.stringify(response[0]));

               userData.storeUserDataInSession(response);

                        window.location = 'userProfile.html';   
                        return false;                                   
                    },
                    error: function(err){                           
                        //alert(JSON.stringify(err));
                        alert("fail");
                        window.location.href = 'error.html';
                    }           
                });
                return false; 

        });
        //loadJSON(0);
    });
</script> 
个人资料页面中的

javascript

<script>

  $( document ).ready(function() {
       var userData = {
         storeUserDataInSession: function(userData) {
             var userObjectString = JSON.stringify(userData);
             window.sessionStorage.setItem('userObject',userObjectString)
         },
         getUserDataFromSession: function() {
             var userData = window.sessionStorage.getItem('userObject')
             return JSON.parse(userData);
         }
        } 

        var userDataObject=userData.getUserDataFromSession();
        var data=userDataObject[0].userInfo.username;           
        alert(data);            
  }); 
</script>

此个人资料页面成功完全能够打印HardcodedInfo&#34; Neelabh&#34;,我不知道如何将JSON数据阵列发送到其他页面请检查我提到 Json数据数组

PHP代码

$result=mysql_query($query);
            $totalRows=mysql_num_rows($result); 
            if($totalRows>0){
                $recipes=array();
                while($recipe=mysql_fetch_array($result, MYSQL_ASSOC)){
                    $recipes[]=array('userInfo'=>$recipe);
                }
                echo json_encode($recipes);         
            }   

上面的代码是工作代码感谢Mateusz Majewski

1 个答案:

答案 0 :(得分:2)

var userData = {
 storeUserDataInSession: function(userData) {
     var userObjectString = JSON.stringify(userData);
     window.sessionStorage.setItem('userObject',userObjectString)
 },
 getUserDataFromSession: function() {
     var userData = window.sessionStorage.getItem('userObject')
     return JSON.parse(userData);
 }
}

然后在你的success()回调中做

userData.storeUserDataInSession(response);

检索您运行的数据

userData.getUserDataFromSession()