如何在javascript fetch()方法中使用url传递变量?

问题描述 投票:-9回答:3

我正试图在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}')

像这样

enter image description here

提前致谢,

javascript fetch fetch-api
3个回答
0
投票

您是否包含了整个URL(在'1'键左侧的这些小家伙)围绕您的JavaScript使用这个${your-variable}


-1
投票
fetch('/api/staffattendance/'+makedate+'')

简单的连接对我来说就像这样


-1
投票

你也可以将te字符串放在这些符号``中,然后在你需要变量的地方加上$ {yourVariable},例如:

  fetch(`/api/staffattendance/${makedate}`)
    .then(res => res.json())
    .then(res => {
      //this.staffs = res.data;
      console.log(res.data);
    })
    .catch(err => console.log(err));
© www.soinside.com 2019 - 2024. All rights reserved.