0

I am trying to pass value to another page and read the value in the another page. I use only JavaScript and jQuery. Not use others language as PHP. I don't want to be as like (http://www.example.com/?value=test). I am trying to use POST or GET method.

On first page:

$.post("second_page.html",{q:"test"});
window.location.replace("second_page.html");

On second page:

var location_query = window.location.search.substring(1);
    var vars = location_query.split("&");
    for (var i in vars) {
        var param = vars[i].split("=");
        params[param[0].toString()] = param[1];
    }

If I pass value as like http://www.example.com/?q=test , the second page script is read the value, but I use this line: $.post("second.html",{q:"test"}); not read.

How to pass value from first page to another static page via JavaScript or jQuery or ajax?

Amir
  • 1,328
  • 2
  • 13
  • 27
cguzel
  • 1,673
  • 2
  • 19
  • 34
  • 1
    The problem is that your `post` is one request, and your `location.replace` is a second request - which is a different instance. Therefore the first post data is not available in the second instance – musefan Nov 12 '13 at 09:47
  • You need to use a GET request as POST puts the parameters in the HTTP body of the request NOT the url – heymega Nov 12 '13 at 09:55
  • Following post might help you: http://stackoverflow.com/questions/12544496/retrieving-values-passed-by-post-method-form It references http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp – Khurram Ishaque Nov 12 '13 at 10:03

3 Answers3

1

See this post says:

POST data is data that is handled server side. And Javascript is on client side. So there is no way you can read a post data using JavaScript.

but you can use cookies to read and store values:

<script src="/path/to/jquery.cookie.js"></script>

you can get it from here

Try with reading the documentation and see if this works for you.

Community
  • 1
  • 1
Jai
  • 74,255
  • 12
  • 74
  • 103
1

Without using GET or POST you can transfer the content using HTML5 Local Storage or Web SQL

yashhy
  • 2,856
  • 5
  • 31
  • 57
0

$.post and $.get send AJAX request and waiting response on current page, you cant send params for next window.location.replace by this.

Use url get-params window.location.replace("404.html?q=404"); and look this for get params on second page.

Community
  • 1
  • 1
akrasman
  • 83
  • 4