How to Read the Post Request Parameters Using JavaScript

How to read the post request parameters using JavaScript

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.

How to read post parameters using JS/Jquery

Client side code has no access to most of the data in the request that was used to load the page the client side code is being executed in, including POST data.

You'll need to use server side code instead.

How to get GET request parameters in JavaScript?

Update June 2021:

Today's browsers have built-in APIs for working with URLs (URL) and query strings (URLSearchParams) and these should be preferred, unless you need to support some old browsers or Opera mini (Browser support).

Original:

All data is available under

window.location.search

you have to parse the string, eg.

function get(name){
if(name=(new RegExp('[?&]'+encodeURIComponent(name)+'=([^&]*)')).exec(location.search))
return decodeURIComponent(name[1]);
}

just call the function with GET variable name as parameter, eg.

get('foo');

this function will return the variables value or undefined if variable has no value or doesn't exist

add parameters to a post request in js

I think this SO answer is a nice and clean way to do it. Basically, it constructs the URL string with parameters.

fetch(url_ticket + new URLSearchParams({
token: `${token}`,
id: `${id}`,
id_secret: `${id_secret}`
}), {});

You could also construct the parameters in a more manual way, by inserting the variables into a backticked URL string:

let url = `${url_ticket}?token=${token}&id=${id}&id_secret=${id_secret}`;

fetch(url, {...})

How can I access parameters from a POST (or other HTTP) request in a C# server?

You can create a class that represents the request:

public class PushStatusRequest
{
public string Order { get; set; }
public string Status { get; set; }
}

And use it like this:

[HttpPost]        
public void PushStatus([FromBody] PushStatusRequest request){
//request.Status
//request.Order
}

JavaScript post request like a form submit

Dynamically create <input>s in a form and submit it

/**
* sends a request to the specified url from a form. this will change the window location.
* @param {string} path the path to send the post request to
* @param {object} params the parameters to add to the url
* @param {string} [method=post] the method to use on the form
*/

function post(path, params, method='post') {

// The rest of this code assumes you are not using a library.
// It can be made less verbose if you use one.
const form = document.createElement('form');
form.method = method;
form.action = path;

for (const key in params) {
if (params.hasOwnProperty(key)) {
const hiddenField = document.createElement('input');
hiddenField.type = 'hidden';
hiddenField.name = key;
hiddenField.value = params[key];

form.appendChild(hiddenField);
}
}

document.body.appendChild(form);
form.submit();
}

Example:

post('/contact/', {name: 'Johnny Bravo'});

EDIT: Since this has gotten upvoted so much, I'm guessing people will be copy-pasting this a lot. So I added the hasOwnProperty check to fix any inadvertent bugs.

Read POST query string from JavaScript

Server side handles POST data.

And javascript is client side. so, it is not possible by javascript



Related Topics



Leave a reply



Submit