我有一个AngularJS应用程序。服务器端是Go并使用Gorilla Web Toolkit mux和会话包。 Angular应用程序在主页面上有两个表单,登录和注册。使用AngularJS $http.post
将数据作为JSON发布到Go,并将相应的响应作为JSON从服务器发回。我想要实现的是,应该在网站的主页面上提供两个不同的页面,具体取决于用户是否登录。目前,当我提交登录表单的详细信息并且服务器以适当的响应进行响应时,我重新加载页面,但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 Code 下面,SignInHandler在POST上调用" / signIn"并且在Get to" /"。
上调用IndexHandlertype 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")
}
}
答案 0 :(得分:1)
在我的IndexHandler中添加“Cache-Control”:“no-store”标题后,它开始正常运行。
w.Header().Set("Cache-Control", "no-store")