I am building a Golang web app. When i redirect after sending a JavaScript GET request, http.Redirect runs the function but load the web template.
I have http.Redirect else where i the code and it works fine can you see anything wrong with me code
This is the JavaScript
function logout() {
var xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", "/logout", true); // true for asynchronous
xmlHttp.send();
}
This is the Go function
func lgout(w http.ResponseWriter, req *http.Request) {
in, sess := alreadyLoggedIn(req)
if in == true {
cookie, _ := req.Cookie("session")
session, _ := mgo.DialWithInfo(&mgo.DialInfo{
Addrs: []string{"cluster1",
"cluster2",
"cluster3",
},
Username: dbUsername,
Password: dbPassword,
DialServer: func(addr *mgo.ServerAddr) (net.Conn, error) {
return tls.Dial("tcp", addr.String(), &tls.Config{})
},
})
defer session.Close()
c := session.DB("bd").C("col")
err := c.Remove(bson.M{"c_id": sess.SessionId})
if err != nil{
panic(err)
}
cookie = &http.Cookie{
Name: "session",
Value: "",
MaxAge: -1,
}
http.SetCookie(w,cookie)
http.Redirect(w,req,"/", 302)
return
}
Thank you
Aucun commentaire:
Enregistrer un commentaire