0

I'm sending an XMLHttpRequest.

In the open function I want to set some parameters for the request.

req.open('GET', /some/path?someparam=*somevariable*, true)

I want to get someparam to equal the value of a variable, like var somevariable. In fact the variable name might even have the same name as someparam.

How do I get the variable to resolve in this instance?

Mars
  • 4,677
  • 8
  • 43
  • 65

1 Answers1

1

String concatenation and encodeURIComponent to make sure you are producing a valid URI:

'/some/path?someparam=' + encodeURIComponent(somevariable)

In an ES6 environment you can use template literals:

`/some/path?someparam=${encodeURIComponent(somevariable)}`
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143