AngularJS 页面不从服务器刷新
AngularJS page does not refresh from server
我有一个 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代码
下面,SignInHandler 在 POST 上被调用到“/signIn”,IndexHandler 在 Get 上被调用到“/”。
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")
}
}
在我的 IndexHandler 中添加 "Cache-Control": "no-store" header 后它开始正常运行。
w.Header().Set("Cache-Control", "no-store")
我有一个 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代码 下面,SignInHandler 在 POST 上被调用到“/signIn”,IndexHandler 在 Get 上被调用到“/”。
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")
}
}
在我的 IndexHandler 中添加 "Cache-Control": "no-store" header 后它开始正常运行。
w.Header().Set("Cache-Control", "no-store")