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

报告相同问题?

悬赏问题

  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面