2015-05-09 53 views
0

我有一個AngularJS應用程序。服務器端是Go並使用Gorilla Web Toolkit mux和會話包。 Angular應用程序在主頁上有兩種形式,登錄和註冊。使用AngularJS $http.post作爲JSON將數據發佈到Go,並將相應的響應作爲JSON從服務器發回。我想要實現的是,根據用戶是否登錄,應該在網站的主頁上提供兩個不同的頁面。目前,當我提交登錄表單的詳細信息並且服務器響應相應的響應時,我重新加載頁面,但AngularJS一直使用表單而不是新頁面顯示頁面。AngularJS頁面不會從服務器刷新

AngularJS代碼

angular.module('app', []) 

angular.module('app').controller('SignInController', ['$scope', '$http', function($scope, $http) { 
    $scope.formData = {} 

    $scope.signIn = function() { 
     $http.post('/signIn', { 
      email: $scope.formData.email, 
      password: $scope.formData.password 
     }).success(function(data) { 
      console.log(data) 
      if(data.ok == true) { 
       window.location.reload(true) 
      } 
     }) 
    } 
}]) 

相關Go代碼 下面,SignInHandler被稱爲一個POST到 「/簽到」 和IndexHandler被調用上獲取到 「/」。

type JsonResponse map[string]interface{} 

func (jr JsonResponse) String() (output string) { 
    b, err := json.Marshal(jr) 
    if err != nil { 
     output = "" 
     return 
    } 
    output = string(b) 
    return 
} 

func SignInHandler(w http.ResponseWriter, r *http.Request) { 
    session, _ := sessionStore.Get(r, "user-session") 

    decoder := json.NewDecoder(r.Body) 
    var user User 
    err := decoder.Decode(&user) 
    if err != nil { 
     fmt.Fprint(w, JsonResponse{"ok": false, "message": "Bad request"}) 
     return 
    } 

    if user.Email == "" || user.Password == "" { 
     fmt.Fprint(w, JsonResponse{"ok": false, "message": "All fields are required"}) 
     return 
    } 

    userExists, u := user.Exists() 
    if userExists == false { 
     fmt.Fprint(w, JsonResponse{"ok": false, "message": "Email and/or password in invalid"}) 
     return 
    } 

    err = bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(user.Password)) 
    if err != nil { 
     fmt.Fprint(w, JsonResponse{"ok": false, "message": "Email and/or password in invalid"}) 
     return 
    } 

    session.Values["userId"] = u.Id.Hex() 

    session.Save(r, w) 

    fmt.Fprint(w, JsonResponse{"ok": true, "message": "Authentication Successful"}) 
} 

func IndexHandler(w http.ResponseWriter, r *http.Request) { 
    session, _ := sessionStore.Get(r, "promandi-user-session") 

    if _, ok := session.Values["userId"]; ok { 
     http.ServeFile(w, r, "./views/home.html") 
    } else { 
     http.ServeFile(w, r, "./views/index.html") 
    } 
} 
+1

您是否檢查會話cookie是否在頁面重新加載時發回? – CuriousMind

+0

你有看過小提琴手發生了什麼嗎? –

回答

1

在我的IndexHandler中添加「Cache-Control」:「no-store」標題後,它開始正常運行。

w.Header().Set("Cache-Control", "no-store")