shriek
shriek

Reputation: 5823

Is there a way to get the form data easily with JavaScript?

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?

Upvotes: 0

Views: 175

Answers (2)

Jeremy J Starcher
Jeremy J Starcher

Reputation: 23863

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.

Upvotes: 2

Alex Wayne
Alex Wayne

Reputation: 187064

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.

Upvotes: 1

Related Questions