GitHub Pages网站链接无法跳转及405 Not Allowed问题求助
问题解决方案
一、链接无法跳转(显示本地路径)问题
你的<a>标签链接格式错误,导致浏览器识别为本地文件路径,修正方式如下:
错误示例:
<a href="petonstepens.github.io/cookies/forgotPassword.html">password?</a> <a href="petonstepens.github.io/cookies/createAccount.html">Create One</a>
两种修正方案:
- 使用完整绝对URL:添加
https://前缀,确保指向正确的GitHub Pages地址:
<a href="https://petonstepens.github.io/cookies/forgotPassword.html">password?</a> <a href="https://petonstepens.github.io/cookies/createAccount.html">Create One</a>
- 使用相对路径(若文件在同一仓库对应目录下):以仓库根目录为基准,直接写相对路径(前提是
cookies文件夹确实在根目录下):
<a href="/cookies/forgotPassword.html">password?</a> <a href="/cookies/createAccount.html">Create One</a>
二、表单提交405 Not Allowed错误
GitHub Pages仅支持静态文件托管,不处理任何后端请求(包括POST),所以用method="post"提交表单会触发405错误,解决方案如下:
方案1:前端处理登录逻辑(推荐)
完全用JavaScript在前端完成验证,不依赖表单提交到服务器:
- 修改表单标签,阻止默认提交行为:
<form onsubmit="logIn(); return false;">
- 修正
logIn函数,实现前端验证与跳转:
function logIn() { const inputUname = document.querySelector('input[name="uname"]').value; const inputPsw = document.querySelector('input[name="psw"]').value; // 读取本地存储的账号密码(假设之前已通过注册逻辑存入) const savedUname = localStorage.getItem("uname"); const savedPsw = localStorage.getItem("psw"); if (inputUname === savedUname && inputPsw === savedPsw) { window.location.href = "homepage.html"; } else { alert("账号或密码错误"); } }
- 移除提交按钮的
onclick属性,补充按钮文本:
<button type="submit">登录</button>
方案2:改用GET方法(仅适用于无敏感信息场景)
将表单method改为get,但这种方式会把账号密码暴露在URL中,不建议用于登录场景:
<form action="homepage.html" method="get">
额外代码修正
你的原始代码中存在标签闭合错误:
<!-- 错误写法 --> <label for="psw"></label><b>Password</b></label> <!-- 修正后 --> <label for="psw"><b>Password</b></label>
内容的提问来源于stack exchange,提问作者peton
相关产品推荐
相关产品推荐

