简体   繁体   English

JS 检测不到表单提交

[英]JS does not detect form submission

 $(document).ready(function() { $("#loginForm").on('submit', function() { var mail = document.getElementById("mail").value; var password = document.getElementById("password").value; req = $.ajax({ url: '/api/login', type: 'POST', data: { email: email, password: password } }); req.done(function(data) { if (data.result == "failed") { let messageHandler = document.getElementById("message-handler"); messageHandler.innerHTML = `<h3> username or password incorrect </h3>`; } }); return false; }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form action="" method="POST"> <input type="mail" id="mail" name="mail"> <input type="text" id="password" name="password"> <input type="submit" id="loginForm"> </form> <div id="message-handler"> </div>

When I click the button, it simply says Method not allowed because I am sending a post request from form.当我单击该按钮时,它只是说不允许使用方法,因为我正在从表单发送一个发布请求。 The js never detects the on submit event. js 永远不会检测到 on submit 事件。

Thanks谢谢

What happens here in /api/login? /api/login 这里发生了什么? Try to point a file like form.php or something else.尝试指向 form.php 之类的文件或其他文件。

req = $.ajax({
      **url: '/api/login',**
      type: 'POST',
      data: {
        email: email,
        password: password
      }
    });

Maybe this is the path you need to follow for your answer;)也许这是您需要遵循的答案;)

Use action='url' Or action='#' this will help to detect your request in browser.使用 action='url' 或 action='#' 这将有助于在浏览器中检测您的请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM