gpt4 book ai didi

ajax - 无法从ajax请求中获取Golang中的post参数

转载 作者:数据小太阳 更新时间:2023-10-29 03:09:34 27 4
gpt4 key购买 nike

在客户端我有代码:

    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")的值?

最佳答案

此代码从请求正文中解码 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
}

关于ajax - 无法从ajax请求中获取Golang中的post参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53978746/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com