为什么此请求不发布?

时间:2020-11-08 00:05:37

标签: go post

在不同的堆栈中实现了许多类似的问题,但是我没有找到有用的答案。该代码以许多不同的教程为模型,这里是其中之一:https://divyanshushekhar.com/golang-forms-data-request-body/

应该按下按钮来提交表单,以便可以使用dataRequest的值,但是它似乎没有发布。为什么?

<!DOCTYPE html>
<meta charset="utf-8" http-equiv="Content-Security-Policy" content="img-src * 'self' data: https:">
<head>
  <title>Demo</title>
  <link rel="icon" type="image/png" href="./favicon.ico">
</head>

<body style="background-color:#F0F8FF" style="font-size: 18px; font-family:verdana,arial,tahoma,serif;">

<form name="demoForm" style="background-color:#92a8d1">
  <fieldset> 
    <input id="dataRequest" type="request" method="POST" value=""/>
    <button type="submit" value="Run" />
  </fieldset>
</form> 


</body>
</html>
package main

import (
    "fmt"
    "net/http"
    "log"
)


func index() http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        switch r.Method {
        case "GET":     
            http.ServeFile(w, r, "visible/index.html")
            return 
        case "POST":
            fmt.Println("Posted a request!")
            err := r.ParseForm(); 
            if err != nil {
                log.Fatal(err)
            }
            request := r.Form.Get("dataRequest")
            fmt.Println(request)
            return
        }
    })
}



func main() {
    mux := http.NewServeMux()
    mux.Handle("/", index())
    http.ListenAndServeTLS(":443", "./server.crt", "./server.key", mux)
}

0 个答案:

没有答案