Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read the post request parameters using JavaScript

I am trying to read the post request parameters from my HTML. I can read the get request parameters using the following code in JavaScript.

$wnd.location.search 

But it does not work for post request. Can anyone tell me how to read the post request parameter values in my HTML using JavaScript?

like image 877
DonX Avatar asked Sep 11 '09 04:09

DonX


People also ask

Can JavaScript read POST data?

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

Can we use parameters in POST request?

In a POST request, the parameters are sent as a body of the request, after the headers. To do a POST with HttpURLConnection, you need to write the parameters to the connection after you have opened the connection.

Can JavaScript send POST request?

To send a POST request using vanilla JavaScript, you can use an XMLHttpRequest object to interact with the server and provide the correct Content-Type request header for the POST message body data.


2 Answers

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.

like image 65
rahul Avatar answered Oct 03 '22 10:10

rahul


A little piece of PHP to get the server to populate a JavaScript variable is quick and easy:

var my_javascript_variable = <?php echo json_encode($_POST['my_post'] ?? null) ?>; 

Then just access the JavaScript variable in the normal way.

Note there is no guarantee any given data or kind of data will be posted unless you check - all input fields are suggestions, not guarantees.

like image 40
Roger Avatar answered Oct 03 '22 09:10

Roger