我有一个登录页面(login.html),它使用Javascript在后端调用golang服务。成功登录后,应将用户带到仪表板页面(dashboard.html)。相反,仪表板页面会在登录页面的中间打开,如下所示:
相关代码是:
func userhandler(w http.ResponseWriter, r *http.Request, subcommand string) {
...
// If the credentials are valid, go to the Dashboard page
if validateLoginCredentials([]byte(loginUsername), []byte(loginPassword)) != -1 {
http.Redirect(w, r, "/dashboard.html", 302) //<<<<<<<<<The Redirect is here
}
...
}
func webhandler(w http.ResponseWriter, r *http.Request) { ...
// Service the API
if strings.HasPrefix(r.URL.Path, "/xyzzy/") {
urlparts := strings.SplitN(r.URL.Path, "/", 5) ...
switch urlparts[3] {
case "user":
userhandler(w, r, urlparts[4]) ...
}
}
} else { // Serve the static pages
http.FileServer(http.Dir(".")).ServeHTTP(w, r)
}
...
}
func main {
...
http.HandleFunc("/", webhandler) ...
err_https := http.ListenAndServeTLS(":3001", "./xyzzy.crt", "./xyzzy.key", nil)
...
}
如果作为测试,我将重定向放到它为静态页面提供服务的位置(所有静态页面将重定向到仪表板页面),它看起来像是有效的。但是,在处理用户api函数的代码中,比如login,我得到了这种行为。
HTML代码段:
<body>
<div class="logintopspacer" id="logintopspacerid"></div>
<div class="loginlogo" id="loginlogoid"><img src="img/pixmover1.png" alt="PixMover" width="270"></div>
<div class="logintitlecontainer" id="logintitlecontainerid">
<div class="logintitle" id="logintitleid">User Login</div>
</div>
<div class="loginmidspacer" id="loginmidspacerid"></div>
<div class="logincredentialscontainer" id="logincredentialscontainerid">
<div class="logincredentialsspacer"></div>
<div class="logincredentialtitle">Username/Email:</div>
<div class="logincredentialentry"><input type="text" name="pixun" id="pixunid" size="25"></div>
<div class="logincredentialsspacer"></div>
<div class="logincredentialsspacer"></div>
<div class="logincredentialtitle">Password:</div>
<div class="logincredentialentry"><input type="password" name="pixpw" id="pixpwid" size="25"></div>
<div class="logincredentialsspacer"></div>
</div>
<div class="loginbuttoncontainer" id="loginbuttoncontainerid">
<div class="loginbutton" id="loginbuttonid" onmousedown="buttonPressed();" onmouseup="submitForm();" onmouseout="buttonReleased();" >Login</div>
</div>
<div class="loginerrormessage" id="loginerrormessageid"></div>
<div class="loginbottomspacer" id="loginbottomspacerid"></div>
<div class="pagefooter" id="pagefooterid">
Copyright © XYZZY, Inc. 2016
</div>
</body>
使用Javascript:
function submitForm()
{
document.getElementById("loginbuttonid").className = "loginbutton";
pixlogin();
}
function pixlogin()
{
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function()
{
if (xhttp.readyState == 4 && xhttp.status == 200)
{
document.getElementById("loginerrormessageid").innerHTML = xhttp.responseText;
}
};
xhttp.open("POST", "pixmover/1_0_0/user/login", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send("username=" + document.getElementById("pixunid").value + "&password=" + document.getElementById("pixpwid").value);
}
答案 0 :(得分:0)
我相信这是来自你的
if (xhttp.readyState == 4 && xhttp.status == 200)
{
document.getElementById("loginerrormessageid").innerHTML = xhttp.responseText;
}
当重定向返回时,您将获得整个页面,并且只是将其加载到错误消息中,因为没有条件。你可能想要做一些事情,比如
if (isAuthenticated) {
location.href = "/"; // whatever page to redirect to
}