我如何在html(pug)文件中使用axios?

问题描述 投票:0回答:1

我正在第一个网站上工作。它需要为产品配备过滤系统。我可以获得所需的所有数据,但似乎无法从axios获得结果。错误消息并没有给我太多有关如何解决它的线索。它只是说“未处理的承诺拒绝:SyntaxError:字符串与预期的模式不匹配。”有人可以帮忙吗?如何在我的html文件中使用axios或将文件外部的params数据发送到我可以使用axios的位置。

  script(src='https://unpkg.com/axios/dist/axios.min.js')
  script(src='filter.js')
  script(type='text/javascript' src='script.js')
  script.
    function openNav() {
      document.getElementById("mySidenav").style.width = "275px";
    }

    function closeNav() {
      document.getElementById("mySidenav").style.width = "0";
    }

    async function applyFilter() {
      let temp = document.getElementById('flag_category');
      const category = temp.options[temp.selectedIndex].value;
      temp = document.getElementById('flag_color');
      const color = temp.options[temp.selectedIndex].value;
      temp = document.getElementById('flag_theme');
      const theme = temp.options[temp.selectedIndex].value;
      temp = document.getElementById('flag_flagType');
      const flagType = temp.options[temp.selectedIndex].value;
      temp = document.getElementById('flag_fabricType');
      const fabricType = temp.options[temp.selectedIndex].value;

      if (color !== '') color = `color: '${color}',\n`;
      if (theme !== '') theme = `theme: '${theme}',\n`;
      if (flagType !== '') flagType = `flagType: '${flagType}',\n`;
      if (fabricType !== '') fabricType = `fabricType: '${fabricType}',\n`;
      if (category !== '') category = `category: '${category}',\n`;

      params = color + theme + flagType + fabricType + category;

      const flags = axios({
        method: 'get',
        url: 'http://127.0.0.1:3000api/v1/flags',
        data: { params }
      });
      console.log(flags);
      /*
        res.status(200).render('flagOverview', {
          title: 'Flags',
          products: flags
        });
        */
    };
javascript html axios pug
1个回答
2
投票

我认为问题是此行:

url:'http://127.0.0.1:3000api/v1/flags',

应该是:

url:'http://127.0.0.1:3000/api/v1/flags',

您错过了一个斜线。

© www.soinside.com 2019 - 2024. All rights reserved.