获取没有收到数据的语言形式 [英] Getting No data received for go language form
问题描述
没有数据 这是astaxie的book
的简单形式,当我尝试'/ login'时,收到{无法加载网页,因为服务器没有发送数据。错误代码:ERR_EMPTY_RESPONSE}
以下是代码:
package main
import(
fmt
html / template
net / http
)
func sayhelloName(w http.ResponseWriter,r * http.Request ){
fmt.Fprintf(w,Hello astaxie!)//将数据写入响应
func登录(w http.ResponseWriter,r * http.Request ){
fmt.Println(method:,r.Method)//获取请求方法
if r.Method ==GET{
t,err:= template.New ).Parse(loginHtml)
if err!= nil {
panic(err)
}
const loginHtml =`
< html> ;
< head>
< title>< / title>
< / head>
< body>
< form action =/ loginmethod =post>
用户名:< input type =textname =username>
密码:< input type =passwordname =password>
< input type =submitvalue =Login>
< / form>
< / body>
< / html>
`
} else {
r.ParseForm()
//登录的逻辑部分
fmt.Println(username:,r (password),
}
}
$ func main(){
http.HandleFunc(/,sayhelloName)//设置路由器规则
http.HandleFunc(/ login,登录)
http.ListenAndServe( :9090,nil)//设置监听端口
}
#login.gtpl
< html>
< head>
< title>< / title>
< / head>
< body>
< form action =/ loginmethod =post>
用户名:< input type =textname =username>
密码:< input type =passwordname =password>
< input type =submitvalue =Login>
< / form>
< / body>
< / html>
任何想法?
原始问题几次)是你的 ParseFiles()
函数失败,它无法读取你的模板文件。你不知道这个,因为错误
它返回了你刚才丢弃的。 永远不要这样做! 如果发生错误,至少可以打印错误或者调用 panic(err)
。你会做到这一点,你会立即看到原因。
login.gtpl
文件必须是如果您指定了相对路径,则将它放置在您启动您的应用程序的工作目录中。或者指定一个绝对路径。
您也可以将HTML源代码放入您的Go文件中,直到您将其排除在外:
t,err:= template.New()。解析(loginHtml)
if err!= nil {
panic(err)
}
t.Execute(w,nil)
// ...其余的代码
//在你的代码结尾。去源文件(在login()func之外)插入:
const loginHtml =`
< head>
< title>< / title>
< / head>
< body>
< form action =/ loginmethod =post>
用户名:< input type =textname =username>
密码:< input type =passwordname =password>
< input type =submitvalue =Login>
< / form>
< / body>
< / html>
`
注#1:
因为你的HTML模板只是一个静态的HTML,所以你可以直接将它发送到输出,而不需要构建和执行一个模板:
//而不是调用template.New(...)。Parse(...)和t.Execute(...),只需要:
w.Write([] byte(loginHtml))
注意2: $ b 另外,您可以使用 It's the simple form from astaxie's book
when I try '/login' , I get Here's the code: main.go
The problem with your original question (which has been edited several times) was that your The You can also put your HTML source into your Go file like this until you sort things out: Note #1: Since your HTML template is just a static HTML, in its current form you can simply just send it to the output without building and executing a template from it: Note #2: The As an alternative you can use the
这篇关于获取没有收到数据的语言形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! Request.Form
仅在 Request.ParseForm( )
已经被调用,所以在访问它之前要这么做。同样,对于 POST
表单,您可能希望使用 Request.PostForm
。
请求。 PostFormValue()
方法,如果它尚未被调用,它会自动执行此操作:
fmt.Println(username:,r.PostFormValue(username))
fmt.Println(password:,r.PostFormValue(password))
No Data received { Unable to load the webpage because the server sent no data. Error code: ERR_EMPTY_RESPONSE }
package main
import (
"fmt"
"html/template"
"net/http"
)
func sayhelloName(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello astaxie!") // write data to response
}
func login(w http.ResponseWriter, r *http.Request) {
fmt.Println("method:", r.Method) //get request method
if r.Method == "GET" {
t, err :=template.New("").Parse(loginHtml)
if err != nil {
panic(err)
}
const loginHtml = `
<html>
<head>
<title></title>
</head>
<body>
<form action="/login" method="post">
Username:<input type="text" name="username">
Password:<input type="password" name="password">
<input type="submit" value="Login">
</form>
</body>
</html>
`
} else {
r.ParseForm()
// logic part of log in
fmt.Println("username:", r.PostFormValue("username"))
fmt.Println("password:", r.PostFormValue("password"))
}
}
func main() {
http.HandleFunc("/", sayhelloName) // setting router rule
http.HandleFunc("/login", login)
http.ListenAndServe(":9090", nil) // setting listening port
}
#login.gtpl
<html>
<head>
<title></title>
</head>
<body>
<form action="/login" method="post">
Username:<input type="text" name="username">
Password:<input type="password" name="password">
<input type="submit" value="Login">
</form>
</body>
</html>
Any idea??
ParseFiles()
function failed, it was not able to read your template file. You didn't know about this because the error
it returned you just discarded. Never do that! The least you can do is print the error or call panic(err)
if it occurs. Would you have done that, you would have seen the cause immediately.login.gtpl
file has to be placed in the working directory you start you app from if you specify a relative path. Or specify an absolute path.t, err := template.New("").Parse(loginHtml)
if err != nil {
panic(err)
}
t.Execute(w, nil)
// ... the rest of your code
// At the end of your .go source file (outside of login() func) insert:
const loginHtml = `
<html>
<head>
<title></title>
</head>
<body>
<form action="/login" method="post">
Username:<input type="text" name="username">
Password:<input type="password" name="password">
<input type="submit" value="Login">
</form>
</body>
</html>
`
// Instead of calling template.New(...).Parse(...) and t.Execute(...), just do:
w.Write([]byte(loginHtml))
Request.Form
is only available after Request.ParseForm()
has been called, so do that prior to accessing it. Also for POST
forms you might want to use Request.PostForm
instead.Request.PostFormValue()
method which does this for you automatically if it has not yet been called:fmt.Println("username:", r.PostFormValue("username"))
fmt.Println("password:", r.PostFormValue("password"))