2016-05-27 3 views
0

JavaScript(javascript)を使用してバックエンドでgolangサービスを呼び出すログインページ(login.html)があります。ログインに成功すると、ユーザーはダッシュボードページ(dashboard.html)に移動する必要があります。Golang httpリダイレクト異常な動作

Page within a Page instead of Redirect

関連するコードは次のとおりです:ここに示したように代わり、ダッシュボードページには、ログインページの途中で開き、テストとして、私はにリダイレクトを入れ、場合

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関数を扱うコードでは、この動作が発生します。

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 &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); 
} 
+0

あなたはどんな種類のテンプレートパッケージやフロントエンドフレームワークを使用していますか? Angular/React/EmberJS? – Datsik

+0

いいえ、違いがあれば、これはWindows上にあります。まっすぐなHTMLとJavascriptです。これは開発中で、URLとしてhttps:// localhost:3001/login.htmlを使用しています。すべてが同じマシン上で実行されています。 – user1426181

+0

ログインを処理するJavaScriptとhtmlのスニペットを含めることができます – Datsik

答えて

0

私は、リダイレクトを使用すると、ページ全体を取得している戻ってくるときに、これはあなたの

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

から来ていると考えていると、条件文がないため、エラーメッセージにロードするだけです。あなたの代わりに何かをしたいかもしれません。

if (isAuthenticated) { 
    location.href = "/"; // whatever page to redirect to 
} 
+0

あなたは素晴らしいです。ありがとう。まだそのページを開発しており、まだエラー状態を処理していません。その行動様式は私を驚かせた。 – user1426181

+0

@ user1426181右上に、gl! – Datsik

+0

実際、私はまだこれに固執しています。はい、それはページの内容でdivを埋めていますが、私はまだこれをやっている理由を把握しようとしています。なんらかの理由で、httpステータスコードをリダイレクトに変更していません。このリダイレクトは、302としてあります。http://www.Redirect(w、r、 "/dashboard.html"、302)それを理解したいのですが、私はあなたの提案を受け取り、location.href解決策を実行します。 – user1426181

関連する問題