doujia6433 2013-06-05 16:41
浏览 393
已采纳

Web.go如何进行http重定向?

I am writing a small app using web.go with a function that serves a form. If the form is not valid, user will be redirected to the same page:

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
     }
}

This works, with one caveat that when the form is not valid, it first shows a page with the text "Not Acceptable" that is liked to /mypage. How can I make it to go directly to /mypage in case the form is not valid? I suspect that it is related to http.StatusNotAcceptable but I don't know what I should replace it with.

  • 写回答

1条回答 默认 最新

  • douzhushen_9776 2013-06-05 17:56
    关注

    So turns out it was easy :)

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

    No need for http.Redirect. The important part is to change the Method to GET first, before calling mypage(ctx).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败