0

I'm using JSON format to send some data between two pages. In the second page I need to get the JSON i have encoded in the URL to add some data in it and send it again to a 3rd page. this is the code I used to send the JSON using GET to the second page.

    form.submit( function(e) {
        // stop the regular form submission
        e.preventDefault();

        // collect the form data
        var data = {};

        data["valueDimensional"] = $('#valueDimensional').val();
        data["timeSlot"] = $('#timeSlot option:selected').val();
        data["splitOption"] = $('#splitOption option:selected').val()

        var strJSON = JSON.stringify(data);
        var escapedStrJSON = encodeURIComponent(strJSON);
        var url = "/warning/app/data?par="+escapedStrJSON;
        window.location.href = url;

     });

In the second page i tried this:

     form.submit( function(e) {
            // stop the regular form submission
            e.preventDefault();

            $.getJSON(window.location.href, function(data) {
                //data is the JSON string
                alert(data);
            });

        });
Falangher
  • 3
  • 3
  • Sorry for offtop: did you ever heard of other ways to store data between pages? On client: cookies, localStorage or server side: session vars? That may be more easy to implement, debug and support than reinvent the wheel with JSON encoded in URL – shershen Mar 25 '16 at 10:59
  • I know these solutions exists, at first I used session vars but then I've been asked to use this solution because there is need to have all the data on the URL – Falangher Mar 25 '16 at 11:03
  • Second page code doesn't make any sense. Ignores form data and how does server know to send json instead of html for that whole page? Not clear at all what you are trying to do – charlietfl Mar 25 '16 at 11:04
  • This is my first question here on StackOverflow, should i edit the question inserting what i'm trying to accomplish? What i need to do is to send a JSON between the pages, but at each page i need to add new data to the same JSON and send it again to the next page. – Falangher Mar 25 '16 at 11:08

2 Answers2

0

Use JSON.parse(window.location.href) in second page code instead of:

$.getJSON(window.location.href, function(data) {
shershen
  • 9,875
  • 11
  • 39
  • 60
0

getJSON takes a URL and makes an HTTP request to it.

It does not search through the URL in order to try to find something that looks like JSON encoded directly into it.

You need to:

  1. parse the query string to read the value of par
  2. Pass the result through JSON.parse.
Community
  • 1
  • 1
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335