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

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

报告相同问题?

悬赏问题

  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥15 关于超局变量获取查询的问题
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集
  • ¥15 在启动roslaunch时出现如下问题
  • ¥15 汇编语言实现加减法计算器的功能
  • ¥20 关于多单片机模块化的一些问题
  • ¥30 seata使用出现报错,其他服务找不到seata
  • ¥35 引用csv数据文件(4列1800行),通过高斯-赛德尔法拟合曲线,在选取(每五十点取1点)数据,求该数据点的曲率中心。