How To Pass A Variable With Url On Javascript Fetch() Method?
I'm trying to fetch data from URL with GET method on javascript, fetch('/api/staffattendance/{makedate}') .then(res => res.json()) .then(res => {
Solution 1:
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.
Solution 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));
Solution 3:
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?
Solution 4:
fetch('/api/staffattendance/'+makedate+'')
Simply concatenation works for me like this
Post a Comment for "How To Pass A Variable With Url On Javascript Fetch() Method?"