Home > Enterprise >  fetch request in JavaScript with dynamic parameters
fetch request in JavaScript with dynamic parameters

Time:11-14

Is there is some way to do a fetch request in JavaScript with dynamic parameters?

for example: '''fetch (/api/$parameter1/$parameter2)'''
when both parameters are declared in the code above them (it determine in the previous code).

CodePudding user response:

You can use template literals, it allows you to interpolate strings with variables. (more here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals)

let parameter1 = 'example-param1';
let parameter2 = 'example-param2';
fetch (`/api/${parameter1}/${parameter2}`)
  • Related