亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無法從ajax請求中獲取Golang中的post參數

無法從ajax請求中獲取Golang中的post參數

Go
慕姐8265434 2023-05-22 17:26:01
在客戶端我有代碼:    let response = await fetch('/getInfo', {       credentials: 'same-origin',       method: 'POST',       body: JSON.stringify({filename: "file.jpg"})     });服務器端的代碼:    fmt.Println(c.PostForm("filename")) // empty為什么是空的?如何獲得的價值c.PostForm("filename")?
查看完整描述

1 回答

?
慕仙森

TA貢獻1827條經驗 獲得超8個贊

此代碼從請求正文中解碼 JSON 對象:


// Request is structure to encode request body

type Request struct {

    FileName string `json:"filename"`

}


// ServeHTTP is request handler

func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {

    decoder := json.NewDecoder(r.Body)

    var req Request

    err := decoder.Decode(&req)

    if err != nil {

        // handle error

        return

    }

    // process request

}


查看完整回答
反對 回復 2023-05-22
  • 1 回答
  • 0 關注
  • 173 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號