0

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).

1 Answer 1

3

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}`)
Sign up to request clarification or add additional context in comments.

1 Comment

You are right. Fixed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.