AngularJS 页面不从服务器刷新

标签 angularjs http go gorilla

我有一个 AngularJS 应用程序。服务器端是 Go 并使用 Gorilla Web Toolkit mux 和 session 包。 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 页面不从服务器刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30136801/

相关文章:

javascript - Post 方法抛出错误

javascript - 如何从相同 ng-options 的其他用途中删除 select ng-options

java - 我们可以使用 Http Post 下载 CSV 文件吗?

json - 如何在 Go 语言中读取 JSON

谷歌云数据流作业失败,出现错误 'Failed to retrieve staged files: failed to retrieve worker in 3 attempts: bad MD5...'

go - 检测未初始化的结构字段

javascript - angular.module 和 window.angular.module 之间有什么区别吗?

javascript - 带有 Angular 指令的 TreeView

http - 用于实现 HTTP 服务器的 RFC

java - Java 中的 HttpPost 不接受过长的 url