首页 > 解决方案 > Firebase Auth 在 HTML 的 Form 标签内不起作用

问题描述

的HTML:



    <form >
        <input id="email" type="text" placeholder="username">
        <input id="pass" type="password" placeholder="password">
        <button onclick="login()">login</button>
        <p class="message">Not registered? <a href="home.html">Create an account</a></p>
    </form>


    </div>

JS:

function login(){

     var userE=document.getElementById("email").value;
     var userP=document.getElementById("pass").value;



     firebase.auth().signInWithEmailAndPassword(userE, userP).catch(function(error) {
       // Handle Errors here.
       var errorCode = error.code;
       var errorMessage = error.message;

       alert(errorMessage);
     });


     firebase.auth().onAuthStateChanged(function(user) {
       if (user) {

           window.location.href="home.html";
       }


     });



 }


当输入字段不在<form>或没有时,授权有效<form>。但是一旦我将它引入HTML,授权就不起作用了。它一遍又一遍地将我重定向到同一个登录页面。

标签: javascripthtmlfirebase

解决方案


当您单击提交按钮时:

  1. JS函数被调用
  2. 表格已提交

提交表单会触发表单提交的 HTTP 响应中的页面导航。

离开当前页面会停止正在其中运行的 JS,因此异步 JS 调用永远不会完成。

用于preventDefault停止表单的正常提交。


推荐阅读