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")
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀