0

I was looking on how to get values of form data that are passed with POST requests and found this answer. I know you you can get GET query parameters quite easily in JavaScript doing window.location.search but is there a way to do similar for POST request or document.forms is my only option?

1
  • 3
    JavaScript can't access the POST request. Commented Apr 30, 2014 at 18:01

2 Answers 2

2

To expand on what RUJordan said:

When you do a POST, the information is sent to the server using an entirely different method and does not show up in the URL or anywhere else that JavaScript can get access to it.

The server, and only the server, can see it.

Now, it is possible to take some of the form data and fill JavaScript variables and/or hidden form fields so the server can pass data back down to the client.

If you need more help, you'd be better off opening another question explaining exactly what problem you are trying to solve.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm going to try out that hidden approach and will open up another question if needed. Thanks.
1

Do you want javascript to see the data was POSTed to load the current page?

JavaScript does not have access to the request body (where the POST content is) that loaded the page. If you want to be able to interact with the POSTed parameters, the server that received the request would need to respond with the necessary data written back out on the page where javascript can find it. This would be done after the form was submitted, as part of the response to that POST request.

Or do you want to know what your page could POST form the forms that are on it?

Inspecting document.forms will let you see what could be POSTed later if those forms were submitted. This would be done before the form was submitted, without a request being made.

2 Comments

The first one and I'm just now realizing that this is something that can only be done from server side.
@shriek Yep you need server side help then. The server needs to expose those variables explicitly to the page. How to do that depends greatly on your server side language and your goals of implementation.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.