英文:
How to redirect to a url
问题
我想在从上一页收集到数据后向客户端展示另一个页面。但是我在服务器端重定向新的URL时遇到了问题。以下是我的逻辑:
- 使用POST方法将用户输入提交到服务器;
- 服务器运行saveChoice()函数将用户输入保存到数据库中;
- 用户输入保存后,服务器向客户端发送一个新的URL;
- 当客户端获取到新的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"])
/* 第3步:让用户打开另一个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()函数。
英文:
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:
- submit user input with POST action to server;
- server run function saveChoice() to save user input into a database;
- After user input is saved, server send a new URL to client;
- By the time the client GET the new URL, server reads the database and get saved data out
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 */
}
}
And here is the template:
<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. If I put URL on button, then server is not going to run saveChoice()
答案1
得分: 66
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,这将导致多次调用响应写入器。
英文:
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)
}
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>
Notice action
of the form is defined as /postHandler
. Put the name of the endpoint that runs your saveChoice
function there.
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)
}
}
Otherwise, the server attempts to render both the form and the redirected url which will result in mutiple calls to the response writer.
答案2
得分: 3
包 main
导入 (
"net/http"
"html/template"
)
类型数据 struct {
列表字符串
}
功能主要() {
http.HandleFunc("/", 检查)
}
功能检查(w http.ResponseWriter, r * http.Request) {
如果 r.Method == "GET" {
sinfo := 数据{
列表: "这里是文件列表",
}
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)
}
如果 r.Method == "POST" {
保存选择(r.Form["choices"])
http.Redirect(w, r, newUrl, http.StatusSeeOther)
}
}
英文:
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)
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论