英文:
How to move html data to struct in Golang
问题
我是新手,正在尝试将数据从一个操作字段移动到一个结构体中。
我的主函数是:
func main() {
http.HandleFunc("/public/", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, r.URL.Path[1:])
})
http.HandleFunc("/public/new_user/", signup)
http.ListenAndServe(":8080", nil)
}
接收来自signup.html
的注册数据的结构体如下:
type Signup struct {
name string
email string
}
将数据接收到上述结构体的函数如下:
func signup(w http.ResponseWriter, r *http.Request) {
var S Signup
S.name = r.FormValue("name")
S.email = r.FormValue("email")
// 仅用于测试输出
fmt.Println("%v\n", S.name)
fmt.Println("%v\n", S.email)
}
我在代码中使用了http.HandleFunc("/public/new_user/", signup)
来渲染signup
函数。然而,当我路由到public/new_user
时,我收到一个空白的HTML页面,而不是从signup
函数接收到的结构体。
上述代码中的S.name = r.FormValue("name")
和S.email = r.FormValue("email")
这两行是从以下signup.html
页面获取信息的:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Sign Up</title>
</head>
<body>
<h1>Sign Up</h1>
<br/>
<form action="/public/new_user" method="post" id="form1">
<label>Name:</label>
<input type="string" name="name" />
</br>
</br>
<label>Email:</label>
<input type="string" name="email" />
</form>
</br>
<button type="submit" form="form1" value="Submit">Submit</button>
</body>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</html>
正如你所看到的,该页面在提交后路由到public/new_user
。请问为什么public/new_user
是空白的?我已经在代码中使用了http.HandleFunc("/public/new_user/", signup)
来渲染signup
函数。谢谢!
英文:
I'm new to Golang and I am trying to move data from an action field to a struct.
My func main is:
func main () {
http.HandleFunc("/public/", func (w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, r.URL.Path[1:]) })
http.HandleFunc("/public/new_user/", signup)
http.ListenAndServe(":8080", nil)
}
// struct for recieving Signup data input from signup.html
type Signup struct {
name string
email string
}
// func to receive data into struc above FROM signup.html
func signup(w http.ResponseWriter, r *http.Request) {
var S Signup
S.name = r.FormValue("name")
S.email = r.FormValue("email")
//this is just to test the output
fmt.Println("%v\n", S.name)
fmt.Println("%v\n", S.email)
}
I have http.HandleFunc("public/new_user/", signup)
which should render the signup
function. However, I am receiving a BLANK html page when I route to public/new_user
instead of receiving the struct from func signup.
The S.name = r.FormValue("name")
and S.email = r.FormValue("email")
lines ABOVE are getting their info from the signup page which is:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Sign Up</title>
</head>
<body>
<h1>Sign Up</h1>
<br/>
<form action="/public/new_user" method="post" id="form1">
<label>Name:</label>
<input type="string" name="name" />
</br>
</br>
<label>Email:</label>
<input type="string" name="email" />
</form>
</br>
<button type="submit" form="form1" value="Submit">Submit</button>
</body>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</html>
As you can see, this page routes to public/new_user
upon submit. Can anyone please tell me why public/new_user
is blank seeing as how I have public/new_user
in:
http.HandleFunc("/public/new_user/", signup)
Which should render the signup function? Thanks
答案1
得分: 1
第一个问题是你将处理程序注册为模式"/public/new_user/"
,但你将表单提交到/public/new_user
。
你需要将HTML的<form>
更改为使用这个URL:
<form action="/public/new_user/" method="post" id="form1">
这样做的重要性在于,即使访问URL/public/new_user
可以工作,因为会自动重定向(301 Moved Permanently
响应),但提交的表单在浏览器发出第二个请求到新URL(以斜杠结尾)时不会被“携带”过去。
在这样做之后,你仍然会收到一个空白页面,因为你的signup()
函数没有写入任何响应(尽管你现在可以在控制台上看到提交和打印的表单数据)。
如果你想看到一个响应,要么写入一些内容,要么重定向到另一个页面。
还要注意,fmt.Println()
没有格式字符串参数,它只会打印你传递给它的所有内容。如果你想使用格式字符串,可以使用fmt.Printf()
。另外,你可以使用%+v
格式字符串打印带有字段名的完整结构体。
例如:
func signup(w http.ResponseWriter, r *http.Request) {
var S Signup
S.name = r.FormValue("name")
S.email = r.FormValue("email")
fmt.Printf("%+v\n", S)
fmt.Fprintf(w, "Form received, you used name: %s", S.name)
}
英文:
The first problem is that you registered your handler for the pattern "/public/new_user/"
but you direct your form post to /public/new_user
.
You have to change your HTML <form>
to use this URL:
<form action="/public/new_user/" method="post" id="form1">
The importance of this is that even though visiting the URL /public/new_user
works because an automatic redirect (301 Moved permanently
response) happens, but the submitted form will not be "carried" over when your browser makes the 2nd request to the new URL (ending with a slash).
After doing this you will still receiving a blank page because your signup()
function does not write any response (although you will now see the submitted and printed form data on your console).
If you want to see a response, either write something or redirect to another page.
Also note that fmt.Println()
does not have a format string parameter, it just prints everything you pass to it. Use fmt.Printf()
if you want to use a format string. Also you can use the %+v
format string to print a complete struct with field names.
For example:
func signup(w http.ResponseWriter, r *http.Request) {
var S Signup
S.name = r.FormValue("name")
S.email = r.FormValue("email")
fmt.Printf("%+v\n", S)
fmt.Fprintf(w, "Form received, you used name: %s", S.name)
}
答案2
得分: 0
我认为你正在跳过表单验证。你必须检查客户端输入,并且应该添加令牌参数以防止XSRF攻击。
你可以使用这个包来验证用户的输入。
英文:
i think you are skipping form validation. You must check client input and you should put token parameter for prevent Xsrf attack.
You can use this package for validate user's inputs.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论