提问人:ubakara samy 提问时间:6/22/2018 最后编辑:xxxubakara samy 更新时间:5/9/2022 访问量:14533
如何在javascript fetch()方法上传递带有url的变量?
How to pass a variable with url on javascript fetch() method?
问:
我正在尝试在 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));
我怎样才能在这里传递变量:
fetch('/api/staffattendance/{makedate}')
喜欢这个
提前致谢,
答:
0赞
ubakara samy
6/22/2018
#1
fetch('/api/staffattendance/'+makedate+'')
简单的串联像这样对我有用
评论
0赞
Prathamesh More
4/1/2020
这不是一个解决方案。
2赞
andres aguirre gonzalez
12/22/2018
#2
您还可以将字符串放在反引号 '' 中,并包含您想要的变量,例如:${yourVariable}
fetch(`/api/staffattendance/${makedate}`)
.then(res => res.json())
.then(res => {
//this.staffs = res.data;
console.log(res.data);
})
.catch(err => console.log(err));
评论
0赞
Prathamesh More
4/1/2020
如何在ejs中使用它?
1赞
ThirdGhostHand
2/28/2019
#3
您是否在 JavaScript 中使用它时将整个 URL 包含在 acutes(“1”键左侧的这些小家伙)中?${your-variable}
6赞
Shreyak Upadhyay
9/14/2019
#4
一种方法是字符串连接,但引入了另一种机制,称为:js
template literal
使用 '' 嵌入字符串,使用 ${makedate} 嵌入用户变量。
`/api/staffattendance/${makedate}`
如果有帮助,请告诉我。
评论
0赞
Manpreet Dhiman
4/7/2021
嗨,大家好,我尝试了其中提到的所有解决方案。但没有任何效果。我想获取“vall”id的数据,请帮帮我。fetch('performancetestdmb.azurewebsites.net/api/products/...)
上一个:字典列表的运行总计
评论
`my_string${my_variable}`
模板文字