Can JavaScript running in a browser access the values POSTed to it's page?

244 views Asked by At

Possible Duplicate:
How to read the post request parameters using javascript

Imagine two webpages running on different webservers.

  • Page-A: Has a < form method="POST" action="http://server-b/page-b" >
  • Page-B: Handles the submission of the form from Page-A

Can JavaScript running in Page-B access the input field values POSTed from Page-A?


An important constraint is that the server that delivers Page-B cannot substitute the POSTed values into Page-B. So JavaScript can only find out via DOM or other browser APIs.

The reason for this constraint is that Page-B in production requires that the field names match a certain form. However, during testing Page-B must be delivered by a different platform, one that does not support the field name format required by the production server. We cannot use the same platform as production due to licensing and other constraints. We could probably spin up another server to serve the Page-B test page, but that's a lot of work and we'd like to avoid it.

Please do not give an answer specific to a platform (PHP, Ruby, whatever). The systems involved are (essentially) proprietary.

2

There are 2 answers

2
RichieHindle On

The short answer is No. The browser is only aware of the content of Page B, not the request that accessed it.

If Page B can't be made to contain the values that were POSTed to it, then the only way for JavaScript on that page to obtain those values would be some completely other route, eg. by having the server log them and then having Page B ask the server via an AJAX request or something equally complex and fragile. Keeping all that in sync could be hard.

0
Mike Christensen On

The short answer is No.

The POSTed parameters are part of the HTTP request that is processed by the server, and is communication directly between the browser (on the page doing the posting) and the server.

Once a server response is rendered out, those HTTP request headers are lost.

There's a few solutions to your problem:

  1. Have Page A remember the POST'ed values, in the form of a cookie or what not.
  2. Have the server render out the POST'ed values as JavaScript variables, or perhaps hidden FORM fields.
  3. Use a GET instead of a POST, as those values could be read directly off the URL.

Hope this helps!