加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何在Go lang中获取ajax post请求值?

发布时间:2020-12-16 03:06:37 所属栏目:百科 来源:网络整理
导读:我在Go工作.以下代码来处理客户端请求. package mainimport ( "net/http" "fmt" )func main() { http.HandleFunc("/",func(w http.ResponseWriter,r *http.Request) { fmt.Fprintf(w,"htmlheadscriptfunction createGroup(){var xmlhttp,number = document.g
我在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",r *http.Request) {
    fmt.Println(r)
    //Try to get the user information
 })
 panic(http.ListenAndServe(":8080",nil))
}

注意

客户端:包含两个文本框以获取电话号码,电子邮件和createGroup按钮.

>如果用户单击createGroup,则使用ajax触发/ createGroup的发布请求.
> createGroup请求在服务器(Go)中处理

问题

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

我已经在/ createGroup处理程序中打印了请求,但是缺少值.

Output: &{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>}

任何帮助将不胜感激.

使用 ParseFormr.FormValue,例如:
http.HandleFunc("/createGroup",r *http.Request) {
    r.ParseForm()
    fmt.Println(r.Form)
    fmt.Println(r.FormValue("email"))
})

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读