dqby43944 2015-01-25 17:00
浏览 54
已采纳

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

I am just starting with learning web development, Go, and Ajax but I am having trouble seeing what is going wrong. I am trying to simply send data back and forth between the client and the server. With the Ajax request, I am sending data from the form to the server but it does not seem to reach the server because the log doesn't print "in posthandler" which leads me to think something is wrong with the ajax request. Attached is the main.go, index.html, and js/getData.js with all the relevant code.

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/
value 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")
}

index.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();
}
  • 写回答

1条回答 默认 最新

  • dongrenzheng1619 2015-01-25 17:48
    关注

    There are two things:

    • No handler present to render assest (in this case js/.)
    • Form by itself get submitted due to "submit" HTML element.

    here is your updated code

    main.go

    package main
    
    import (
        "fmt"
        "io/ioutil"
        "log"
        "net/http"
    )
    
    var INDEX_HTML []byte
    
    func main() {
        fmt.Println("starting server on http://localhost:8888/
    value is %s", "asdf")
        http.HandleFunc("/", IndexHandler)
        http.HandleFunc("/post", PostHandler)
        serveSingle("/js/getData.js", "./js/getData.js")
        http.ListenAndServe(":8888", nil)
    }
    
    func serveSingle(pattern string, filename string) {
        http.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {
            http.ServeFile(w, r, filename)
        })
    }
    
    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")
    }
    

    index.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="button" name="button" id="button" value="Send" onclick="loadXMLDoc()"/>
        </form>
        <div id="fromserver">
        </div>
      </body>
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?