-4

I'm trying to fetch data from URL with GET method on javascript,

fetch('/api/staffattendance/{makedate}')
  .then(res => res.json())
  .then(res => {
    //this.staffs = res.data;
    console.log(res.data);
  })
  .catch(err => console.log(err));

How can I pass the variable here:

fetch('/api/staffattendance/{makedate}')

Like this

enter image description here

Thanks in Advance,

3
  • 1
    Did you search a bit? Maybe `my_string${my_variable}` is what you are looking for. Or string concatenation. There are plenty of solution available and well documented Commented Jun 22, 2018 at 7:50
  • 2
    Looks like you're trying to use Template Literals Commented Jun 22, 2018 at 8:01
  • Does this answer your question? Most efficient way to concatenate strings in JavaScript? Commented Jan 17, 2020 at 17:42

4 Answers 4

6

One method is the string concatenation but js has introduced another mechanism called template literal:

Embed the string with `` and user variable with ${makedate}.

`/api/staffattendance/${makedate}`

Let me know if this helps.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi guys, I tried all solution mention in this. but nothing worked. I want to fetch data for "vall" id, please help me. fetch('performancetestdmb.azurewebsites.net/api/products/…)
2

You can also put the string in backticks ``, and include ${yourVariable} where you want your variable, example:

  fetch(`/api/staffattendance/${makedate}`)
    .then(res => res.json())
    .then(res => {
      //this.staffs = res.data;
      console.log(res.data);
    })
    .catch(err => console.log(err));

1 Comment

How can I use this in ejs?
1

Did you encompass the whole URL in acutes ( these little guys to the left of your '1' key) WHILE using this ${your-variable} around your JavaScript?

Comments

0
fetch('/api/staffattendance/'+makedate+'')

Simply concatenation works for me like this

1 Comment

This is not a solution.

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.