dongpo0409 2016-05-27 21:33
浏览 170
已采纳

Golang HTTP重定向奇怪的行为

I have a login page (login.html) that uses Javascript to call a golang service on the back end. Upon successful login, the user should be taken to a dashboard page (dashboard.html). Instead, the dashboard page opens up in the middle of the login page as shown here:

Page within a Page instead of Redirect

The relevant code is:

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)
... 
}

If, as a test, I put the redirect into the spot where it serves the static pages (all static pages will redirect to the dashboard page), it looks like it works. But, in the code that handles the user api functions, like login, I get this behavior.

HTML Snippet:

<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 &copy; 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);
}
  • 写回答

1条回答 默认 最新

  • dongshuxi3105 2016-05-27 22:35
    关注

    I believe this is coming from your

    if (xhttp.readyState == 4 && xhttp.status == 200)
    {
        document.getElementById("loginerrormessageid").innerHTML = xhttp.responseText;
    }
    

    When the redirect comes back you're getting the entire page and it's just loading it in to the error message because there is no conditionals. You might want to do something instead like

    if (isAuthenticated) {
        location.href = "/"; // whatever page to redirect to
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型