首页 > 技术文章 > go接收前端传过来的方式

lxz123 2021-09-02 10:27 原文

get请求方式:

func marixExplainPHandler(w http.ResponseWriter,rr *http.Request){
  query := rr.URL.Query()
  id :
= query.Get("id")
}

form表单传递参数

func marixExplainPHandler(w http.ResponseWriter,rr *http.Request){
  con := rr.PostFormValue("con")
  lang := rr.PostFormValue("lang")
 }

post请求正常接收参数:

func marixExplainPHandler(w http.ResponseWriter,request *http.Request){
  decoder := json.NewDecoder(request.Body)

    // 用于存放参数key=value数据
    var params map[string]string

    // 解析参数 存入map
    decoder.Decode(&params)

    fmt.Printf("POST json: username=%s, password=%s\n", params["username"], params["password"])
 }

 

推荐阅读