[英]How to pass a variable with url on javascript fetch() method?
I'm trying to fetch data from URL with GET method on javascript,我正在尝试使用 javascript 上的 GET 方法从 URL 获取数据,
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 variable here我如何在这里传递变量
fetch('/api/staffattendance/{makedate}')
Like this像这样
Thanks in Advance,提前致谢,
One method is the string concatenation but js
has introduced another mechanism called template literal
:一种方法是字符串连接,但
js
引入了另一种称为template literal
机制:
Embed the string with `` and user variable with ${makedate}.用 `` 嵌入字符串,用 ${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:您还可以将字符串放在反引号 `` 中,并在您想要变量的位置包含
${yourVariable}
,例如:
fetch(`/api/staffattendance/${makedate}`)
.then(res => res.json())
.then(res => {
//this.staffs = res.data;
console.log(res.data);
})
.catch(err => console.log(err));
在 JavaScript 周围使用${your-variable}
,您是否将整个 URL 包含在尖括号中(“1”键左侧的这些小家伙)?
fetch('/api/staffattendance/'+makedate+'')
像这样简单地连接对我有用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.