在客戶端我有代碼: 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
}
- 1 回答
- 0 關注
- 173 瀏覽
添加回答
舉報
0/150
提交
取消