2013-06-05 50 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