如何重定向到一个网址

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

  1. 使用 POST 操作向服务器提交用户输入;

  2. 服务器运行函数 saveChoice() 将用户输入保存到数据库中;

  3. 用户输入保存后,服务器向客户端发送一个新的 URL;

  4. 当客户端 GET 新 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>

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


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


慕田峪4524236
浏览 151回答 2
2回答

BIG阳

http 状态 303 是此处的适当响应。所以用它重定向请求。if r.Method == "POST" {&nbsp; &nbsp; saveChoice(r.Form["choices"])&nbsp; &nbsp; http.Redirect(w, r, newUrl, http.StatusSeeOther)}如果您newUrl应该向浏览器返回正确的 html 页面,则不需要使用 ajax。使用 html 表单。<form action="/postHandler" method="post">&nbsp; &nbsp;{{range .List}}&nbsp; &nbsp; <input type="checkbox" name="choices" value="{{.}}"> <span>{{.}}</span><br>&nbsp; &nbsp;{{end}}&nbsp; &nbsp; <input type="submit" value="Submit"></form>通知action的形式定义为/postHandler。将运行您的saveChoice函数的端点的名称放在那里。因此,为了避免http: multiple response.WriteHeader calls错误,您可以使用此代码。&nbsp; func checkcheck(w http.ResponseWriter, r *http.Request) {&nbsp; &nbsp; if r.Method == "GET" {&nbsp; &nbsp; &nbsp; sinfo := Stuff{&nbsp; &nbsp; &nbsp; &nbsp; List: some_slice&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; t, err := template.New("").Parse(tpl_ds)&nbsp; &nbsp; &nbsp; checkErr(err)&nbsp; &nbsp; &nbsp; err = r.ParseForm()&nbsp; &nbsp; &nbsp; checkErr(err)&nbsp; &nbsp; &nbsp; err = t.Execute(w, sinfo)&nbsp; &nbsp; &nbsp; checkErr(err)&nbsp; &nbsp; }&nbsp; &nbsp; if r.Method == "POST" {&nbsp; &nbsp; &nbsp; &nbsp; saveChoice(r.Form["choices"])&nbsp; &nbsp; &nbsp; &nbsp; http.Redirect(w, r, newUrl, http.StatusSeeOther)&nbsp; &nbsp; }&nbsp; }否则,服务器会尝试同时呈现表单和重定向的 url,这将导致对响应编写器的多次调用。

呼啦一阵风

package mainimport (&nbsp; "net/http"&nbsp; "html/template")type data struct {&nbsp; List string}func main() {&nbsp; http.HandleFunc("/", check)}func check(w http.ResponseWriter, r * http.Request) {&nbsp; if r.Method == "GET" {&nbsp; &nbsp; sinfo: = data {&nbsp; &nbsp; &nbsp; List: "Here is a list of the files Located with in",&nbsp; &nbsp; }&nbsp; &nbsp; var tpl_ds = "index.html"&nbsp; &nbsp; //t, err := template.New("").Parse(tpl_ds)&nbsp; &nbsp; t: = template.Must(template.ParseFiles(tpl_ds))&nbsp; &nbsp; r.ParseForm()&nbsp; &nbsp; t.Execute(w, sinfo)&nbsp; }&nbsp; if r.Method == "POST" {&nbsp; &nbsp; saveChoice(r.Form["choices"])&nbsp; &nbsp; http.Redirect(w, r, newUrl, http.StatusSeeOther)&nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go