使用Golang和Bootstrap时,表单数据始终为空

使用Golang和Bootstrap时,表单数据始终为空,go,Go,所以我尝试使用bootstrap创建一个表单并在golang中解析它,但是表单数据总是返回空的 戈兰: // CreateEmployee - handler function for creating a new employee func CreateEmployee(w http.ResponseWriter, r *http.Request) { err := r.ParseForm() if err != nil { fmt.Println(err

所以我尝试使用bootstrap创建一个表单并在golang中解析它,但是表单数据总是返回空的

戈兰:

   // CreateEmployee - handler function for creating a new employee
func CreateEmployee(w http.ResponseWriter, r *http.Request) {
    err := r.ParseForm()
    if err != nil {
        fmt.Println(err.Error())
    }
    fmt.Println(r.Form)
}
引导表格:

<html>
<head>
<title></title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
</head>
<body>
    <form action="/employee" method="post">
        <div class="form-group">
            <label>Name</label>
            <input type="text" id="name" class="form-control" placeholder="Enter name">
        </div>
        <div class="form-group">
            <label>Title</label>
            <input type="text" id="title" class="form-control" placeholder="Enter title">
        </div>
        <div class="form-group">
            <label>Email</label>
            <input type="email" id="email" class="form-control" placeholder="Enter email">
        </div>
        <div class="form-group">
            <label>Photo</label>
            <input type="text" id="photo" class="form-control" placeholder="Enter photo 
             location">
        </div>
        <div class="form-group">
            <label>Phonetic</label>
            <input type="text" id="phonetic" class="form-control" placeholder="Enter 
              phonetic pronunciation">
        </div>
        <button type="submit" class="btn btn-primary">Submit</button>
      </form>
</body>

名称
标题
电子邮件
照片
语音的
提交

我知道在填充r.Form之前必须调用ParseForm,但据我所知,这就是所需的全部

该路由工作正常,因为当我提交表单时,它正在向控制台打印一张空地图。似乎我在做一些愚蠢的事情,但却无法理解。

元素中缺少该属性。如果name属性未设置或为空,则提交的表单中不包括输入值

这样修正:

    <div class="form-group">
        <label>Name</label>
        <input type="text" name="name" id="name" class="form-control" placeholder="Enter name">
    </div>

名称
。。。对于其他输入元素,依此类推。

该属性在
元素中丢失。如果name属性未设置或为空,则提交的表单中不包括输入值

这样修正:

    <div class="form-group">
        <label>Name</label>
        <input type="text" name="name" id="name" class="form-control" placeholder="Enter name">
    </div>

名称

。。。其他输入元素也是如此。

Duh。你太棒了。我就知道是那样的蠢事。我今天盯着代码看得太多了,我知道我只是忽略了一些简单的东西。关于调试的提示:在浏览器中打开开发人员控制台,单击“网络”,勾选“保留日志”复选框,然后提交表单。您应该能够看到HTTP请求,并查看参数是否存在。这将帮助您确定问题是在客户端(缺少参数)还是在服务器上(参数看起来与预期的一样)。Duh。你太棒了。我就知道是那样的蠢事。我今天盯着代码看得太多了,我知道我只是忽略了一些简单的东西。关于调试的提示:在浏览器中打开开发人员控制台,单击“网络”,勾选“保留日志”复选框,然后提交表单。您应该能够看到HTTP请求,并查看参数是否存在。这将帮助您确定问题是在客户端(缺少参数)还是在服务器上(参数看起来与预期一致)。