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

huangapple go评论78阅读模式
英文:

Ajax Request not sending to Go web server

问题

我刚刚开始学习网页开发、Go和Ajax,但是我在找出问题所在方面遇到了困难。我试图在客户端和服务器之间简单地发送数据。使用Ajax请求,我将表单中的数据发送到服务器,但似乎没有到达服务器,因为日志没有打印出"in 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")
}

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();
}
英文:

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 (
	&quot;fmt&quot;
	&quot;net/http&quot;
	&quot;io/ioutil&quot;
	&quot;log&quot;
)

var INDEX_HTML []byte

func main(){
	fmt.Println(&quot;starting server on http://localhost:8888/\nvalue is %s&quot;, value)
	http.HandleFunc(&quot;/&quot;, IndexHandler)
	http.HandleFunc(&quot;/post&quot;, PostHandler)
	http.ListenAndServe(&quot;:8888&quot;, nil)
}

func IndexHandler(w http.ResponseWriter, r *http.Request){
	log.Println(&quot;GET /&quot;)
	w.Write(INDEX_HTML)
}

func PostHandler(w http.ResponseWriter, r *http.Request){
	r.ParseForm()
	log.Println(&quot;in posthandler&quot;, r.Form)
	var value = r.FormValue(&quot;textfield&quot;)
	w.Write([]byte(value))
}
func init(){
	INDEX_HTML, _ = ioutil.ReadFile(&quot;./html/index.html&quot;)
}

index.html

&lt;!doctype html&gt;
&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;Page Title&lt;/title&gt;
  &lt;script src=&quot;js/getData.js&quot;&gt;&lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;form action=&quot;/post&quot; method=&quot;post&quot;&gt;
      &lt;textarea type=&quot;text&quot; name=&quot;input&quot; id=&quot;textfield&quot;&gt;&lt;/textarea&gt;
      &lt;br /&gt;
      &lt;input type=&quot;submit&quot; name=&quot;button&quot; id=&quot;button&quot; value=&quot;Send&quot; onclick=&quot;loadXMLDoc()&quot;/&gt;
    &lt;/form&gt;
    &lt;div id=&quot;fromserver&quot;&gt;
    &lt;/div&gt;
  &lt;/body&gt;
&lt;/html&gt;

js/getData.js

function loadXMLDoc() {
    var xmlhttp;
    xmlhttp=new XMLHttpRequest();
    xmlhttp.onreadystatechange=function()
    {
	if (xmlhttp.readyState==4 &amp;&amp; xmlhttp.status==200)
	{
	    document.getElementById(&quot;fromserver&quot;).innerHTML=xmlhttp.responseText;
	}
    }
    xmlhttp.open(&quot;POST&quot;,&quot;post&quot;,true);
    xmlhttp.send();
}

答案1

得分: -1

有两个问题:

  • 没有处理程序来渲染资源(在这种情况下是 js/.
  • 表单本身由于 "submit" HTML 元素而被提交。

以下是您的更新代码:

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/\nvalue 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>

希望对您有所帮助!

英文:

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 (
	&quot;fmt&quot;
	&quot;io/ioutil&quot;
	&quot;log&quot;
	&quot;net/http&quot;
)

var INDEX_HTML []byte

func main() {
	fmt.Println(&quot;starting server on http://localhost:8888/\nvalue is %s&quot;, &quot;asdf&quot;)
	http.HandleFunc(&quot;/&quot;, IndexHandler)
	http.HandleFunc(&quot;/post&quot;, PostHandler)
	serveSingle(&quot;/js/getData.js&quot;, &quot;./js/getData.js&quot;)
	http.ListenAndServe(&quot;:8888&quot;, 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(&quot;GET /&quot;)
	w.Write(INDEX_HTML)
}

func PostHandler(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	log.Println(&quot;in posthandler&quot;, r.Form)
	var value = r.FormValue(&quot;textfield&quot;)
	w.Write([]byte(value))
}
func init() {
	INDEX_HTML, _ = ioutil.ReadFile(&quot;./html/index.html&quot;)
}

index.html

&lt;!doctype html&gt;
&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;Page Title&lt;/title&gt;
  &lt;script src=&quot;js/getData.js&quot;&gt;&lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;form action=&quot;/post&quot; method=&quot;post&quot;&gt;
      &lt;textarea type=&quot;text&quot; name=&quot;input&quot; id=&quot;textfield&quot;&gt;&lt;/textarea&gt;
      &lt;br /&gt;
      &lt;input type=&quot;button&quot; name=&quot;button&quot; id=&quot;button&quot; value=&quot;Send&quot; onclick=&quot;loadXMLDoc()&quot;/&gt;
    &lt;/form&gt;
    &lt;div id=&quot;fromserver&quot;&gt;
    &lt;/div&gt;
  &lt;/body&gt;
&lt;/html&gt;

huangapple
  • 本文由 发表于 2015年1月26日 01:00:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/28138952.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定