【问题标题】:How to redirect to a url如何重定向到一个网址
【发布时间】:2016-03-11 07:14:12
【问题描述】:

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

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

我被困在第 3 步(这里是流程示例):

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>

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

附言如果我将 URL 放在按钮上,那么服务器将不会运行 saveChoice()

【问题讨论】:

  • 其实有效的部分就是像Aruna说的那样加上if r.Method=="GET",这样可以防止重定向2页的错误

标签: go


【解决方案1】:

http 状态 303 是此处的适当响应。所以用它重定向请求。

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

如果您的newUrl 应该向浏览器返回正确的html 页面,则您不需要使用ajax。使用 html 表单。

<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 函数的端点的名称放在那里。

所以要避免http: multiple response.WriteHeader calls 错误,请使用此代码。

  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,这将导致对响应编写器的多次调用。

【讨论】:

  • 起初它显示一个错误,所以我必须在按钮上做 preventDefault
  • Kyle 请同时发布您在此按钮上使用的 javascript/html 代码。
  • Kyle 如果 newUrl 应该返回正确的 html 页面,请不要使用 ajax。只需让表单提交即可。然后重定向将起作用。
  • 我试过没有代码但仍然无法工作。如果我在将数据上传到服务器之前让按钮或表单操作重定向,那么 saveChoice() 将不会运行。让我看看我是否可以添加更多代码,以便您查看详细信息。感谢您的帮助!
  • Kyle,运行saveChoice 的post 请求的url 是什么?你能展示定义这个端点的代码吗?
【解决方案2】:
package main

import (
  "net/http"
  "html/template"
)

type data struct {
  List string
}

func main() {
  http.HandleFunc("/", check)
}

func check(w http.ResponseWriter, r * http.Request) {
  if r.Method == "GET" {
    sinfo: = data {
      List: "Here is a list of the files Located with in",
    }

    var tpl_ds = "index.html"

    //t, err := template.New("").Parse(tpl_ds)
    t: = template.Must(template.ParseFiles(tpl_ds))

    r.ParseForm()
    t.Execute(w, sinfo)
  }

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

【讨论】:

  • 这并不能回答整个问题,但它会导致答案。我希望这会有所帮助。
猜你喜欢
  • 2020-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-31
  • 2016-11-05
  • 2011-02-09
相关资源
最近更新 更多