这段遍历json文件然后检测的代码用不了,admin正常,如何解决?


button.onclick = () => {

    if (document.querySelector('input[name="username"]').value === 'admin' &&
    document.querySelector('input[name="password"]').value === 'admin') {
      // 跳转到admin.html
      window.location.href = './admin.html';
    } else {
      fetch('./up.json')
        .then(response => response.json())
        .then(data => {
          let loginSuccess = false;
  
          data.forEach(item => {
            if (item.username === document.querySelector('input[name="username"]').value &&
             item.password === document.querySelector('input[name="password"]').value) {
              loginSuccess = true;
            }
          });
  
          if (loginSuccess) {
            // 跳转到user.html
            alert('登录成功!');
            window.location.href = './user.html';
          } else {
            alert('登录失败!');
          }
        });
    }
  }
  

这段遍历json文件然后检测的代码用不了,admin正常,但是后面那段无法运行

打印下data,看看是什么数据咯

【相关推荐】




如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^