首页 > 解决方案 > 来自 JS 问题的 HTML FORM 重定向

问题描述

我的网站上有一个表格,每当我使用它时,都会有两种不同的行为:

-1 如果任何字段为空,它会将我重定向到预期的页面

-2 如果两个字段都填满,那么它不会将我重定向到任何地方 HTML

<div id="id01" class="modal">
  <span onclick="document.getElementById('id01').style.display='none'"
class="close" title="Close Modal">&times;</span>

  <!-- Modal Content -->
  <form class="modal-content animate">
    <div class="imgcontainer">
      <img src="Logo_JO_d'été_-_Paris_2024.png" alt="Avatar" class="avatar">
    </div>

    <div class="container">
      <label for="uname"><b>Username</b></label>
      <input type="text" placeholder="Enter Username" id="uname" required>

      <label for="upsw"><b>Password</b></label>
      <input type="password" placeholder="Enter Password" id="upsw" required>

      <button onClick="SendData()">Login</button>
    </div>
  </form>
</div>
    </div>

JS

function SendData()
{
    var XHR = new XMLHttpRequest();
    var urlEncodedData = "";
    var urlEncodedDataPairs = [];
    
    const uName = document.getElementById('uname');
    const uPswd = document.getElementById('upsw');
    
    urlEncodedDataPairs.push(encodeURIComponent("mail") + '=' + encodeURIComponent(uName));
    urlEncodedDataPairs.push(encodeURIComponent("password") + '=' + encodeURIComponent(uPswd));
    urlEncodedDataPairs.push(encodeURIComponent("role") + '=' + encodeURIComponent("user"));
    
    urlEncodedData = urlEncodedDataPairs.join('&').replace(/%20/g, '+');
    
    XHR.open('POST', 'http://XXX.XXX.XXX.XX:XXXX/account/add');
    XHR.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    XHR.send(urlEncodedData);

    sPath = "../booking/bookingen.html"
    window.location = sPath;
}

任何人都可能知道它为什么这样做?

标签: htmljsonformspost

解决方案


推荐阅读