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
4 Answers
Reset to default 6One 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
版权声明:本文标题:How to pass a variable with url on javascript fetch() method? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741618245a2388657.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论