0

I have stored JSON data response from server intooldJson variable. I want to pass this variable data to new page where another JS function is found.

Here is the first page code

$(document).ready(function(){
    $("#submitform").click(function(e)
    {
    var MyForm = JSON.stringify($("#myform").serializeJSON());
    console.log(MyForm);
     $.ajax(
     {
     url : "http://localhost:8080/e-learner/v1/signIn",
     type: "post",
     headers: { 'Content-Type': 'application/json' },
     data : MyForm,

    success:function(data,status){

       console.log(data);
       if(data.status==1)
       {
       alert(data.errorMessage);//alert ur data here by checking data
       }
       else if(data.clientType == "Admin")
       {
         var oldJson = JSON.stringify(data);
         console.log(oldJson);

         $('#temp').data('oldJson', jData);

     // alert("Your account created Successfully. Your Reference Key is"+data.referenceKey); 
     window.location.href = "Report.html"
      //redirect code to dashboard page console.log(JSON.stringify(data));
    }
    else
    {
    window.location.href = "index.html"
}
    },
    error:function(err){
       console.log(err);
       alert(err.statusText);
    }

     });


    });
    });
    </script>

Second page code
$.getJSON('jData', function(jsonData) in this function I want to pass oldJson data variable

<script>
    $(document).ready(function () {

        jData = $('#temp').data('oldJson');
    $.getJSON('jData', function(jsonData) {


    console.log(jsonData);

    var s1=jsonData.data.student;

    for (var i = 0; i < s1.length; i++) {
       var counter = s1[i];
       if(jsonData.status!="error")
            {
                tr = $('<tr/>');
                tr.append("<td>" + counter.referencekey + "</td>");
               tr.append("<td>" + counter.email + "</td>");
                tr.append("<td>" + counter.clients+ "</td>");

                $('table').append(tr);

            }
      else
          {
            alert(jsonData.message);
          }
      }

     });
        });




    </script>
Dayan
  • 7,634
  • 11
  • 49
  • 76
Pranit Bhor
  • 136
  • 2
  • 13

1 Answers1

0

while redirecting use following code

else if(data.clientType == "Admin")
{
    var oldJson = JSON.stringify(data);
    window.location.href = "Report.html?data=" + oldJson;
}

for second page to get value

add following function

function getUrlVars (url) {
            var vars = {};
            url.replace(/[?&]+([^=&]+)=([^&]*)/gi, function (m, key, value) { vars[key] = value; });
            return vars;
        }

and then to get the old value, do following

var urlValues = getUrlVars(window.location.href);
var oldValue = JSON.parse(decodeURIComponent(vars.data));