0

I have two pages and when a used clicks a button in the first one i make an ajax request and redirect to the second page using the following method.

window.location.href="newpage.php";

Now my intention is to use the result of the ajax request into this second page. But it seems when the second page is loaded all the json data from the ajax request is lost. Is there anyway to accomplish this?

altsyset
  • 339
  • 2
  • 20

3 Answers3

3

Like Jamie Taylor sad, don't use AJAX if you are switching pages anyways.

Whatever the code in the file you request with AJAX does, move that code to the beginning of newpage.php.

seymar
  • 3,993
  • 6
  • 25
  • 30
  • I see your point and I will try to refactor my code but for now I'm gone try the local storage suggested by Codemonkey – altsyset Jul 29 '13 at 06:43
2

Use cookies or HTML5 local storage to save the JSON data after fetching it. The data will then be available at any time from the user's browser. A cookie can hold roughly 4KB (though I'm not sure if that is browser-dependent) so if you're saving very small amounts of JSON that may be enough. Otherwise you should use local storage. If I remember correctly, you're guaranteed at least 2.5 MB of data (in Chrome). Other browsers may give you more (read more).

Check out the jQuery localStorage plugin for easy usage of the local storage API.

Hubro
  • 56,214
  • 69
  • 228
  • 381
1

iThe length of an URL is limited. Depending on your json data, using "window.location.href" this may cause errors.

@see What is the maximum length of a URL in different browsers?

You should try creating a <form method='POST' id="yourFormId"> object and populating the form fields with the data you need:

<input type="hidden" id="jsondata" name="jsondata" value="myJsonString">

Then you can trigger the redirection using javascript with "document.getElementById('yourFormId').submit()"

Community
  • 1
  • 1
user2622729
  • 321
  • 2
  • 4