英文:
GoLang/Javascript: Empty postForm and decode(body) on JSON POST
问题
我正在尝试从JavaScript页面向Golang服务器POST JSON数据,但是我无法在两端使用SO接受的答案中找到任何JSON数据的痕迹。
这篇帖子展示了我在JavaScript中发布JSON的方式,而这篇帖子展示了我在Go中处理这个JSON的方式。
//js json post send
var request = new XMLHttpRequest();
request.open('POST', 'http://localhost:8080/aardvark/posts', true);
request.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
var data = {hat: "fez"};
request.send(JSON.stringify(data));
//Go json post response
func reply(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Credentials", "true")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
w.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT")
if err := r.ParseForm(); err != nil {
fmt.Println(err);
}
//this is my first impulse. It makes the most sense to me.
fmt.Println(r.PostForm); //out -> `map[]` would be `map[string]string` I think
fmt.Println(r.PostForm["hat"]); //out -> `[]` would be `fez` or `["fez"]`
fmt.Println(r.Body); //out -> `&{0xc82000e780 <nil> <nil> false true {0 0} false false false}`
type Hat struct {
hat string
}
//this is the way the linked SO post above said should work. I don't see how the r.Body could be decoded.
decoder := json.NewDecoder(r.Body)
var t Hat
err := decoder.Decode(&t)
if err != nil {
fmt.Println(err);
}
fmt.Println(t); //out -> `{ }`
}
我真的不确定接下来还能尝试什么。我应该做出什么改变才能使其工作?
英文:
I'm trying to POST JSON data from a javascript page, into a golang server, but I'm unable to find any trace of the JSON data using the SO accepted answers on both ends.
This post shows the way I'm posting my JSON in Javascript and this post shows the way I'm trying to process this JSON in Go.
//js json post send
var request = new XMLHttpRequest();
request.open('POST', 'http://localhost:8080/aardvark/posts', true);
request.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
var data = {hat: "fez"};
request.send(JSON.stringify(data));
The header below was set from this answer
//Go json post response
func reply(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Credentials", "true")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
w.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT")
if err := r.ParseForm(); err != nil {
fmt.Println(err);
}
//this is my first impulse. It makes the most sense to me.
fmt.Println(r.PostForm); //out -> `map[]` would be `map[string]string` I think
fmt.Println(r.PostForm["hat"]); //out -> `[]` would be `fez` or `["fez"]`
fmt.Println(r.Body); //out -> `&{0xc82000e780 <nil> <nil> false true {0 0} false false false}`
type Hat struct {
hat string
}
//this is the way the linked SO post above said should work. I don't see how the r.Body could be decoded.
decoder := json.NewDecoder(r.Body)
var t Hat
err := decoder.Decode(&t)
if err != nil {
fmt.Println(err);
}
fmt.Println(t); //out -> `{ }`
}
I'm not really sure what else to try from here. What should I change to make this work?
答案1
得分: 3
将结构体Hat
的字段hat
导出,这样JSON解码就可以正常工作。
type Hat struct {
Hat string // 导出的字段名以大写字母开头
}
英文:
Export the field hat
of the struct Hat
and json decoding would work.
type Hat struct {
Hat string // Exported field names begins with capital letters
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论