Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
66
rated 0 times [  73] [ 7]  / answers: 1 / hits: 30314  / 10 Years ago, fri, january 30, 2015, 12:00:00
var JSONonj = {name: James,age:25};
var data = This data i would like to retrieve and print in the main.html page after success call in ajax.;

$.ajax({
url: /user,
type: POST,
data: JSONobj,
dataType: json,
success: function() {
window.location.href = main.html;
},
contentType: application/json
});


Here is the deal. After success i want to redirect to main.html and when I get there, I would like to retrieve and print data variable there.



The redirection works fine. But I can not receive the data there.


More From » ajax

 Answers
23

There are two main ways to achieve that :




  1. somehow send the data to the server (see the other answers for possible ways to do that)

  2. use the browser's localStorage :



    success: function() {
    localStorage.setItem('myMainKey', data);
    window.location.href = main.html;
    }

    // in main.html's javascript :
    var data = localStorage.getItem('myMainKey');
    if (data !== undefined) {
    //do something
    }



Note : if you want to store and retrieve a complete javascript object, you would have to use JSON.stringify() / JSON.parse() for storing / retrieving it.


[#68019] Thursday, January 29, 2015, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kristinsonjab

Total Points: 364
Total Questions: 98
Total Answers: 98

Location: Christmas Island
Member since Mon, Oct 19, 2020
4 Years ago
kristinsonjab questions
Fri, Mar 4, 22, 00:00, 2 Years ago
Fri, Jan 22, 21, 00:00, 3 Years ago
Fri, Aug 14, 20, 00:00, 4 Years ago
;