douhuxi4145 2015-07-07 05:15
浏览 36

如何在Golang中将HTML数据移动到结构

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
", S.name)
    fmt.Println("%v
", 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

  • 写回答

2条回答 默认 最新

  • doukun0888 2015-07-07 05:31
    关注

    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
    ", S)
    
        fmt.Fprintf(w, "Form received, you used name: %s", S.name)
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计