admin管理员组

文章数量:1295941

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

Thanks in Advance,

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

Thanks in Advance,

Share Improve this question edited May 9, 2022 at 9:26 xXx 1,1611 gold badge11 silver badges24 bronze badges asked Jun 22, 2018 at 7:48 ubakara samyubakara samy 1592 gold badges3 silver badges8 bronze badges 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 – Ulysse BN Commented Jun 22, 2018 at 7:50
  • 2 Looks like you're trying to use Template Literals – Alex Commented Jun 22, 2018 at 8:01
  • Does this answer your question? Most efficient way to concatenate strings in JavaScript? – miken32 Commented Jan 17, 2020 at 17:42
Add a ment  | 

4 Answers 4

Reset to default 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.

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

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

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

Simply concatenation works for me like this

本文标签: How to pass a variable with url on javascript fetch() methodStack Overflow