-1

Is it possible to change the name of the data parameters sent with AJAX dynamically? For example:

let ParamName = `Post_${incremented_value}`;

$.post("index.php" { ParamName : "someValue" }); 

When viewing the request params the name is still ParamName.

William
  • 9
  • 2

1 Answers1

1

What you're essentially asking is:

How to set dynamic object property names?

You can do this by adding them via square-bracket syntax.

let params = {};
params[ParamName] = 'someValue';
$.post("index.php" params);

You can also do it via Object.defineProperty() (link):

Object.defineProperty(params, ParamName, {value: 'someValue'});
Mitya
  • 33,629
  • 9
  • 60
  • 107