go-处理AJAX POST请求

go-处理AJAX POST请求,ajax,http,post,go,request,Ajax,Http,Post,Go,Request,我设置了一个中间件和路由器来处理路径/api/on上的AJAX请求,如下所示 http.HandleFunc("/",route.IndexHandler) http.HandleFunc("/login",route.GoogleLoginHandler) http.HandleFunc("/auth/google",route.GoogleCallbackHandler) http.Handle("/dashboard",negroni.New( negroni.Ha

我设置了一个中间件和路由器来处理路径
/api/on
上的AJAX请求,如下所示

http.HandleFunc("/",route.IndexHandler)
http.HandleFunc("/login",route.GoogleLoginHandler)
http.HandleFunc("/auth/google",route.GoogleCallbackHandler)
http.Handle("/dashboard",negroni.New(
            negroni.HandlerFunc(route.IsAuthenticated),
            negroni.Wrap(http.HandlerFunc(route.DashboardHandler)),
))
http.Handle("/api/on",negroni.New(
            negroni.HandlerFunc(route.IsAuthenticated),
            negroni.Wrap(http.HandlerFunc(route.TurnOn)),
))
中间件
IsAuthenticated
允许跳转到下一个路由器(如果我的web中有用户会话),否则它将重定向

func IsAuthenticated(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
    session, _ := util.GlobalSessions.SessionStart(w, r)
    defer session.SessionRelease(w)

    if session.Get("profile") == nil {
        http.Redirect(w, r, "/", http.StatusMovedPermanently)
    } else {
        next(w, r)
    }
}
这是
打开
处理程序,供以后处理

func TurnOn(w http.ResponseWriter, r *http.Request) {
    session, _ := util.GlobalSessions.SessionStart(w, r)
    defer session.SessionRelease(w)

    w.Header().Set("Access-Control-Allow-Origin","*")
    w.Header().Set("Access-Control-Allow-Methods","POST")
    w.Header().Set("Access-Control-Allow-Headers","Content-Type")

    fmt.Println(r.URL)
}

打开服务器后,我使用AJAX使用JavaScript的客户端代码执行POST请求,但控制台没有记录任何内容。即使AJAX请求成功发送,似乎也没有达到
开启
功能。我明显错过了什么吗?

您是否记得将客户端上的内容类型设置为
application/x-www-form-urlencoded
?@我没有,但它在这里什么都没有。我需要检查它是否到达处理程序,它没有,所以问题是它没有到达您的开启函数?您的AJAX调用是否返回错误?404? 超时?什么都没有?@Machiel是的,它似乎没有达到
开启
函数@elithrar No。在JavaScript代码中,我为它编写了一段代码,在控制台中记录一些东西,以通知成功。它会记录日志,这意味着不会发生错误