javascript - 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,授权就不起作用了。它一遍又一遍地将我重定向到同一个登录页面。
解决方案
当您单击提交按钮时:
- JS函数被调用
- 表格已提交
提交表单会触发表单提交的 HTTP 响应中的页面导航。
离开当前页面会停止正在其中运行的 JS,因此异步 JS 调用永远不会完成。
用于preventDefault
停止表单的正常提交。
推荐阅读
- ios - 在 iOS 13 中获取 UIScrollView 的完整屏幕截图
- http - httpClient.get 调用返回 404 Not Found 错误
- forms - 如何避免计算属性中的意外副作用 - VueJS
- ggplot2 - 在 geom_text 中使用 bquote 后跟文本时的下标
- typescript - 按值从打字稿联合中选择元素
- html - 转换的 div 显示在另一个 div 的上方,而它应该始终位于下方(在 Safari 上)
- html - 如何强制 SVG 受容器元素的限制?
- ruby-on-rails - docker compose rails 6 example with mailcatcher 不起作用
- c# - 如何通过 COM 互操作将 VBA 变体数组数据类型传递给 C# 方法
- bash - 尝试从 Raspberry PI DHT 22 传感器的输出中提取数字