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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵