2013-06-05 52 views
1

我正在写一个使用web.go的小型应用程序,它具有一个提供表单的函数。如果表单无效,用户会被重定向到同一页:Web.go我该如何做http重定向?

func mypage(ctx *web.Context) { 
    if ctx.Request.Method == "GET" { 
     // show the form 
    } else if ctx.Request.Method == "POST" { 
     // redirection if the form is not valid: 
     ctx.Request.Method = "GET" 
     http.Redirect(ctx.ResponseWriter, 
         ctx.Request, "/mypage", http.StatusNotAcceptable) 
     return 
    } 
} 

这个方法奏效,有一点需要注意的是,当形式是无效的,它首先显示与被喜欢的文字"Not Acceptable"页面/mypage。如果表单无效,我怎样才能直接转到/mypage?我怀疑它与http.StatusNotAcceptable有关,但我不知道应该用什么替换它。

回答

1

所以原来很容易:)为http.Redirect

ctx.Request.Method = "GET" 
mypage(ctx) 

没有必要。重要的部分是在拨打mypage(ctx)之前先将Method更改为GET