doukun8670 2015-05-09 06:29
浏览 6
已采纳

AngularJS页面无法从服务器刷新

I have an AngularJS app. The server side is Go and uses Gorilla Web Toolkit mux and sessions packages. The Angular app has two forms on the main page, Sign In and Sign Up. The data is posted to Go using AngularJS $http.post as JSON and appropriate responses are sent back from the server as JSON. What I want to achieve is that two different pages should be served on the main page of the website depending on if the user is logged in or not. Currently, when I submit the details of the Sign In form and the server responds with an appropriate response, I reload the page, but AngularJS keeps showing the page with the forms and not the new page.

AngularJS Code

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)
            }
        })
    }
}])

Relevant Go Code Below, the SignInHandler gets called on a POST to "/signIn" and the IndexHandler gets called on a Get to "/".

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条回答 默认 最新

  • douqingnao9246 2015-05-09 12:24
    关注

    It started behaving properly after I added the "Cache-Control": "no-store" header in my IndexHandler.

    w.Header().Set("Cache-Control", "no-store")
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?