如何重定向到URL [英] How to redirect to a url

查看:91
本文介绍了如何重定向到URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从上一页收集到数据后,我想向客户显示另一页.但是我在服务器端重定向新URL时遇到了麻烦.这是我的逻辑:

I would like to show client another page after data has been collected from previous page. But I have trouble redirect the new URL on the server side. Here is my logic:

  1. 将带有POST操作的用户输入提交给服务器;
  2. 服务器运行功能saveChoice()将用户输入保存到数据库中;
  3. 保存用户输入后,服务器将新的URL发送给客户端;
  4. 当客户端获取新URL时,服务器读取数据库并取出保存的数据

我陷入了第3步(这里是流程示例):

And I am stuck on step 3 (here is an example of the flow):

type Stuff struct{
    List []string
}

func checkcheck(w http.ResponseWriter, r *http.Request) {
    sinfo := Stuff{
        List: some_slice
    }

    t, err := template.New("").Parse(tpl_ds)
    checkErr(err)
    err = r.ParseForm()
    checkErr(err)
    err = t.Execute(w, sinfo)
    checkErr(err)

    if r.Method == "POST" {
        saveChoice(r.Form["choices"])
        /* step 3: make user open another URL */
    }
}

这是模板:

<html>
<script>
  $(function () {
    $('form').on('submit', function (e) {
      e.preventDefault();
      $.ajax({
        type: 'post',
        data: $('form').serialize(),
      });
    });
  });
</script>
<body>
  <form method="POST">
    {{range .List}}
        <input type="checkbox" name="choices" value="{{.}}"> <span>{{.}}</span><br>
    {{end}}
    <input type="submit" value="Submit">
  </form>
</body>
</html>

我可以知道如何重定向到新页面吗?

May I know how I can redirect to a new page?

p.s.如果我将URL放在按钮上,则服务器将不会运行saveChoice()

p.s. If I put URL on button, then server is not going to run saveChoice()

推荐答案

http状态303是此处的适当响应.因此,使用它重定向请求.

The http status 303 is the appropriate response here. So redirect the request with it.

if r.Method == "POST" {
    saveChoice(r.Form["choices"])
    http.Redirect(w, r, newUrl, http.StatusSeeOther)
}

并且如果您的 newUrl 应该将正确的html页面返回到浏览器,则无需使用ajax.使用html表单.

And if your newUrl is supposed to return a proper html page to the browser, you don't need to use ajax. Use an html form.

<form action="/postHandler" method="post">
   {{range .List}}
    <input type="checkbox" name="choices" value="{{.}}"> <span>{{.}}</span><br>
   {{end}}
    <input type="submit" value="Submit">
</form>

表格的

通知 action 定义为/postHandler .将在其中运行您的 saveChoice 函数的端点的名称放在此处.

Notice action of the form is defined as /postHandler. Put the name of the endpoint that runs your saveChoice function there.

因此,为了避免 http:多次响应.WriteHeader调用错误,您会使用此代码.

So to avoid http: multiple response.WriteHeader calls error you get use this code.

  func checkcheck(w http.ResponseWriter, r *http.Request) {
    if r.Method == "GET" {
      sinfo := Stuff{
        List: some_slice
      }

      t, err := template.New("").Parse(tpl_ds)
      checkErr(err)
      err = r.ParseForm()
      checkErr(err)
      err = t.Execute(w, sinfo)
      checkErr(err)
    }

    if r.Method == "POST" {
        saveChoice(r.Form["choices"])
        http.Redirect(w, r, newUrl, http.StatusSeeOther)
    }
  }

否则,服务器将尝试同时呈现表单和重定向的url,这将导致对响应编写器的多次调用.

Otherwise, the server attempts to render both the form and the redirected url which will result in mutiple calls to the response writer.

这篇关于如何重定向到URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆