2

I'm using AJAX to fetch JSON data. The user fills out a form with some addresses, and these addresses are posted using AJAX. The response will either be "status": "fail", "status": "unknown" or "status": "success" + a lot of other stuff based on the status. Each status should cause a new page to load with the rest of the response.

My issue is getting the data to the new page.

$.ajax({
  url:"https://domain.local/",
  type:"POST",
  crossDomain: true,
  dataType: "json",
  data : data,
  headers: {
    'Content-Type': "application/json; charset=utf-8"
  },
  success: function(data) {
    if (data.result.Status == " fail ") {
      // head to fail.php and show response
    }

    else if (data.result.Status == " unknown ") {
      // head to unknown.php and show response
    }

    else if (data.result.Status == " success ") {
      // head to succes.php and show response
    }
   }
});

Does anyone know how to do that?

pshoeg
  • 379
  • 2
  • 9
  • 20

3 Answers3

1
  • Make a form in java script dynamically. With an hidden field.
  • Put the data of response in a hidden field.
  • Post to a page that can simple read hidden field value and display it.
  • Force form data to be posted in new tab.
Waqar Alamgir
  • 9,828
  • 4
  • 30
  • 36
  • Wouldn't I then need to add the response to the url to be able to fetch it from the new page? – pshoeg Nov 09 '12 at 12:33
  • no you will put the response in string format in hidden field, use type=post for form, in this way you do not need to append in url, on server side read data from post, for instance in PHP $_POST['hidden_field_name'] – Waqar Alamgir Nov 09 '12 at 21:33
  • It's (as far as I could figure out) not possible to do this with PHP anyway, as the form is submitted using a JS function that then redirects to a success or a fail page. – pshoeg Nov 12 '12 at 10:14
1

Use Cookies:

http://www.w3schools.com/js/js_cookies.asp

Attach own property into Window Object:

http://www.w3schools.com/jsref/obj_window.asp

if (data.result.Status == " fail ") {
  window.myOwnData = data;
  window.location.href = "fail.php";
}

Pass your data as GET parameter of redirect url

if (data.result.Status == " fail ") {
  window.location.href = "fail.php?someData=" + data + "&someOtherData=" + otherData ;
}

EDIT

Or use JSSession, discussion here: Persist javascript variables across pages?

I guess theres much more options

Community
  • 1
  • 1
kamil
  • 3,482
  • 1
  • 40
  • 64
  • using cookies is very bad, as they will be moved through every http call – Waqar Alamgir Nov 09 '12 at 21:36
  • Also, I'm not allowed to use cookies for this service. – pshoeg Nov 12 '12 at 09:22
  • How would I go about using custom properties in the window object? I've tried just creating a string (`window.myOwnData = "test";`) and then on the new page I'm trying to append `window.MyOwnData` to a `div`, but nothing shows. – pshoeg Nov 12 '12 at 10:05
  • properties name missmatch, `myOwnData` and `MyOwnData` – kamil Nov 12 '12 at 10:06
  • Sorry, is miswrote. In my code, though, it is spelled correctly. – pshoeg Nov 12 '12 at 10:07
  • Here's the copied code: `window.Addresses = "test"; window.location.href = "bookTrip2.php";`. Then on bookTrip2.php: `$("#bookTrip").append(window.Addresses);` – pshoeg Nov 12 '12 at 10:09
  • I guess it's not that good solution as it might not work across browser. Try using get parameters, parse them on server side and pass data to next page – kamil Nov 12 '12 at 10:21
  • Is it possible to do this securely, and only on fail? – pshoeg Nov 12 '12 at 10:27
  • Would it be an option to use JSON stringify and then send it using AJAX somehow? – pshoeg Nov 12 '12 at 10:30
  • Found JSSession for you: http://stackoverflow.com/questions/1981673/persist-javascript-variables-across-pages – kamil Nov 12 '12 at 10:49
  • That seems pretty sweet. Thanks, I'll give it at go and get back to you. – pshoeg Nov 12 '12 at 11:22
1

You can use window.location.href to specify the page you wish to redirect

if (data.result.Status == " fail ") {
      window.location.href = "http://xyz.com/fail.php";
    }

    else if (data.result.Status == " unknown ") {
      window.location.href = "http://xyz.com/unknown.php";
    }

    else if (data.result.Status == " success ") {
      window.location.href = "http://xyz.com/success.php";
    }
Prashant Singh
  • 3,725
  • 12
  • 62
  • 106