How to get ajax post request value in Go lang?

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

How to get ajax post request value in Go lang?

问题

我正在使用Go进行工作。以下是处理客户端请求的代码。

package main

import (
    "net/http"
    "fmt"
)

func main() {
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "<html><head><script>function createGroup(){var xmlhttp,number = document.getElementById('phoneNumber').value,email = document.getElementById('emailId').value; var values = {}; values.number = phoneNumber; values.email= emailId; if (window.XMLHttpRequest){xmlhttp=new XMLHttpRequest();}else{xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');}xmlhttp.open('POST','createGroup',true);xmlhttp.send(values.toString());}</script></head><body><h1>Group</h1><input type='text' name='phoneNumber' id='phoneNumber'/><input type='email' id='emailId' name='emailId'/><button onClick='createGroup()'>Create Group</button></body></html>")
    })
    http.HandleFunc("/createGroup", func(w http.ResponseWriter, r *http.Request) {
        fmt.Println(r)
        //尝试获取用户信息
    })
    panic(http.ListenAndServe(":8080", nil))
}

注意

Client: 包含两个文本框用于获取电话号码电子邮件createGroup按钮。

  1. 如果用户点击createGroup,将使用ajax触发/createGroup的POST请求。

  2. 在服务器端(Go)处理createGroup请求。

问题

如何在服务器端获取电话号码和电子邮件?

我在/createGroup处理程序中打印了请求,但是值丢失了。

输出:&{POST /createGroup HTTP/1.1 1 1 map[Accept:[text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8] Accept-Encoding:[gzip, deflate] Content-Length:[15] Content-Type:[text/plain; charset=UTF-8] Connection:[keep-alive] Pragma:[no-cache] Cache-Control:[no-cache] User-Agent:[Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:25.0) Gecko/20100101 Firefox/25.0] Accept-Language:[en-US,en;q=0.5] Referer:[http://localhost:8080/]] 0xc200099ac0 15 [] false localhost:8080 map[] map[] <nil> map[] 127.0.0.1:59523 /createGroup <nil>}

任何帮助将不胜感激。

英文:

I am working in Go. Following code to handle the client request.

package main

import (
    &quot;net/http&quot;
    &quot;fmt&quot;
 )

func main() {
  http.HandleFunc(&quot;/&quot;, func(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, &quot;&lt;html&gt;&lt;head&gt;&lt;script&gt;function createGroup(){var xmlhttp,number = document.getElementById(&#39;phoneNumber&#39;).value,email = document.getElementById(&#39;emailId&#39;).value; var values = {}; values.number = phoneNumber; values.email= emailId; if (window.XMLHttpRequest){xmlhttp=new XMLHttpRequest();}else{xmlhttp=new ActiveXObject(&#39;Microsoft.XMLHTTP&#39;);}xmlhttp.open(&#39;POST&#39;,&#39;createGroup&#39;,true);xmlhttp.send(values.toString());}&lt;/script&gt;&lt;/head&gt;&lt;body&gt;&lt;h1&gt;Group&lt;/h1&gt;&lt;input type=&#39;text&#39; name=&#39;phoneNumber&#39; id=&#39;phoneNumber&#39;/&gt;&lt;input type=&#39;email&#39; id=&#39;emailId&#39; name=&#39;emailId&#39;/&gt;&lt;button onClick=&#39;createGroup()&#39;&gt;Create Group&lt;/button&gt;&lt;/body&gt;&lt;/html&gt;&quot;)
 })
 http.HandleFunc(&quot;/createGroup&quot;,func(w http.ResponseWriter, r *http.Request) {
	fmt.Println(r)
    //Try to get the user information
 })
 panic(http.ListenAndServe(&quot;:8080&quot;, nil))
}

###Note
Client: Contains two text box to get the phone number, email and createGroup button.

  1. If user clicks the createGroup, Post request of /createGroup is triggered using ajax.

  2. createGroup request is handled in server(Go)

###Problem

How to get the phone number and email in server side?

I have printed the request in /createGroup handler but values are missing.

Output: &amp;{POST /createGroup HTTP/1.1 1 1 map[Accept:[text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8] Accept-Encoding:[gzip, deflate] Content-Length:[15] Content-Type:[text/plain; charset=UTF-8] Connection:[keep-alive] Pragma:[no-cache] Cache-Control:[no-cache] User-Agent:[Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:25.0) Gecko/20100101 Firefox/25.0] Accept-Language:[en-US,en;q=0.5] Referer:[http://localhost:8080/]] 0xc200099ac0 15 [] false localhost:8080 map[] map[] &lt;nil&gt; map[] 127.0.0.1:59523 /createGroup &lt;nil&gt;}

Any help will be grateful.

答案1

得分: 25

使用ParseFormr.FormValue进行处理,例如:

http.HandleFunc("/createGroup", func(w http.ResponseWriter, r *http.Request) {
    r.ParseForm()
    fmt.Println(r.Form)
    fmt.Println(r.FormValue("email"))
})
英文:

Use ParseForm and r.FormValue, for example :

http.HandleFunc(&quot;/createGroup&quot;,func(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	fmt.Println(r.Form)
	fmt.Println(r.FormValue(&quot;email&quot;))
})

huangapple
  • 本文由 发表于 2014年4月21日 20:12:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/23197206.html
匿名

发表评论

匿名网友

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

确定