Ajax 请求不发送到 Go Web 服务器

我刚刚开始学习 Web 开发、Go 和 Ajax,但我无法看出哪里出了问题。我试图简单地在客户端和服务器之间来回发送数据。对于 Ajax 请求,我将数据从表单发送到服务器,但它似乎没有到达服务器,因为日志没有打印“在 posthandler”,这让我认为 ajax 请求有问题。附件是 main.go、index.html 和 js/getData.js 以及所有相关代码。


main.go


package main


import (

    "fmt"

    "net/http"

    "io/ioutil"

    "log"

)


var INDEX_HTML []byte


func main(){

    fmt.Println("starting server on http://localhost:8888/\nvalue is %s", value)

    http.HandleFunc("/", IndexHandler)

    http.HandleFunc("/post", PostHandler)

    http.ListenAndServe(":8888", nil)

}


func IndexHandler(w http.ResponseWriter, r *http.Request){

    log.Println("GET /")

    w.Write(INDEX_HTML)

}


func PostHandler(w http.ResponseWriter, r *http.Request){

    r.ParseForm()

    log.Println("in posthandler", r.Form)

    var value = r.FormValue("textfield")

    w.Write([]byte(value))

}

func init(){

    INDEX_HTML, _ = ioutil.ReadFile("./html/index.html")

}

索引.html


<!doctype html>

<html>

  <head>

    <title>Page Title</title>

  <script src="js/getData.js"></script>

  </head>

  <body>

    <form action="/post" method="post">

      <textarea type="text" name="input" id="textfield"></textarea>

      <br />

      <input type="submit" name="button" id="button" value="Send" onclick="loadXMLDoc()"/>

    </form>

    <div id="fromserver">

    </div>

  </body>

</html>

js/getData.js


function loadXMLDoc() {

    var xmlhttp;

    xmlhttp=new XMLHttpRequest();

    xmlhttp.onreadystatechange=function()

    {

    if (xmlhttp.readyState==4 && xmlhttp.status==200)

    {

        document.getElementById("fromserver").innerHTML=xmlhttp.responseText;

    }

    }

    xmlhttp.open("POST","post",true);

    xmlhttp.send();

}


慕后森
浏览 170回答 1
1回答

慕仙森

有两件事:没有处理程序呈现资源(在本例中为 js/ .)由于“提交”HTML 元素,表单本身被提交。这是您更新的代码main.gopackage mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "io/ioutil"&nbsp; &nbsp; "log"&nbsp; &nbsp; "net/http")var INDEX_HTML []bytefunc main() {&nbsp; &nbsp; fmt.Println("starting server on http://localhost:8888/\nvalue is %s", "asdf")&nbsp; &nbsp; http.HandleFunc("/", IndexHandler)&nbsp; &nbsp; http.HandleFunc("/post", PostHandler)&nbsp; &nbsp; serveSingle("/js/getData.js", "./js/getData.js")&nbsp; &nbsp; http.ListenAndServe(":8888", nil)}func serveSingle(pattern string, filename string) {&nbsp; &nbsp; http.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {&nbsp; &nbsp; &nbsp; &nbsp; http.ServeFile(w, r, filename)&nbsp; &nbsp; })}func IndexHandler(w http.ResponseWriter, r *http.Request) {&nbsp; &nbsp; log.Println("GET /")&nbsp; &nbsp; w.Write(INDEX_HTML)}func PostHandler(w http.ResponseWriter, r *http.Request) {&nbsp; &nbsp; r.ParseForm()&nbsp; &nbsp; log.Println("in posthandler", r.Form)&nbsp; &nbsp; var value = r.FormValue("textfield")&nbsp; &nbsp; w.Write([]byte(value))}func init() {&nbsp; &nbsp; INDEX_HTML, _ = ioutil.ReadFile("./html/index.html")}索引.html<!doctype html><html>&nbsp; <head>&nbsp; &nbsp; <title>Page Title</title>&nbsp; <script src="js/getData.js"></script>&nbsp; </head>&nbsp; <body>&nbsp; &nbsp; <form action="/post" method="post">&nbsp; &nbsp; &nbsp; <textarea type="text" name="input" id="textfield"></textarea>&nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; <input type="button" name="button" id="button" value="Send" onclick="loadXMLDoc()"/>&nbsp; &nbsp; </form>&nbsp; &nbsp; <div id="fromserver">&nbsp; &nbsp; </div>&nbsp; </body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go