更新html页面而不在golang中重新加载

时间:2016-05-01 23:35:05

标签: html go

我正在golang中编写我的第一台服务器。我有一个用户将填写的登录表单,我想在页面上显示消息而不重新加载。

到目前为止我所拥有的:

  1. index.tmpl
  2. <form class="form-horizontal" role="form"  action="/auth/login" method="POST">
      <div class="form-group">
        <div class="col-sm-2">
          <label for="inputEmail" class="control-label">Email</label>
        </div>
        <div class="col-sm-10">
          <input type="email" name="email" class="form-control input-lg" id="inputEmail" placeholder="Email">
        </div>
      </div>
      <div class="form-group">
        <div class="col-sm-2">
          <label for="inputPassword" class="control-label">Password</label>
        </div>
        <div class="col-sm-10">
          <input type="password" name="password" class="form-control input-lg" id="inputPassword" placeholder="Password">
        </div>
      </div>
      <div class="form-group">
        <div class="col-sm-12">
          <label for="message" class="control-label text-danger">{{ .message }}</label>
        </div>
      </div>
      <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
          <button type="submit" class="btn btn-default btn-lg">Sign in</button>
        </div>
      </div>
    </form>
    

    在这里,我有一个邮件占位符,我想打印所有错误信息。

    1. server.go
    2. if _, ok := resp.Item["Email"]; ok {
                      if *resp.Item["Email"].S == form.Email && *resp.Item["Password"].S == form.Password {
                          var bucketlist []string
                          for _, dataset := range resp.Item["Datasets"].SS {
                              bucketlist = append(bucketlist, *dataset)
                          }
                          log.Info("User password and email match")
                          c.HTML(http.StatusOK, "bucketlist.tmpl", gin.H{
                              "bucketlist": bucketlist,
                          })
                      } else {
                          log.Info("Failure authorizing user: Invalid login")
                          c.HTML(http.StatusUnauthorized, "index.tmpl", gin.H{
                              "message": "Invalid login information.",
                          })
                      }
                  } else {
                      log.Info("Failure authorizing user: Invalid login")
                      c.HTML(http.StatusUnauthorized, "index.tmpl", gin.H{
                          "message": "Invalid login information.",
                      })
                  }
              }
          } else {
              log.Info("Failure authorizing user: No input provided")
              c.HTML(http.StatusUnauthorized, "index.tmpl", gin.H{
                  "message": "Please fill the form with valid login information.",
              })
          }
      

      在这里,我正在处理错误并在index.tmpl中打印到{{.message}}占位符。如果我输入了错误的密码,我希望网页更新占位符而不重新加载整个页面。有没有办法做到这一点?如果有的话,我们将非常感谢正确方向上的一点。

0 个答案:

没有答案
相关问题