JQuery POST到Golang会损坏字段名称。

huangapple go评论91阅读模式
英文:

JQuery POST to Golang corrupts field names

问题

我有一个包含以下代码行的JQuery脚本:

console.log("Killing "+selected)
$.post("http://127.0.0.1:8080/delete_birloki",{ birloki: selected})

它会输出日志...

Killing france

Golang处理程序以以下方式开始:

func deleteBirloki(rw http.ResponseWriter, req *http.Request) {
    dat,err := httputil.DumpRequest(req,true)
    if err != nil {
	    fmt.Fprintf(rw,"BAD")
	    return
    }
    fmt.Println(string(dat))

输出结果为:

POST /delete_birloki HTTP/1.1
Host: 127.0.0.1:8080
Accept: */*
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Cookie: weve_been_here_before=true; _ga=GA1.1.606577919.1412775218
Origin: http://127.0.0.1:8080
Referer: http://127.0.0.1:8080/config/?birloki=&north=&east=
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36
X-Requested-With: XMLHttpRequest

birloki%5B%5D=france

有人能解释为什么变量名已经改变了吗?在类似的情况下,我可以使用以下方式请求 "birloki[]":

birloki := req.PostFormValue("birloki[]")

但即使在这种情况下也似乎不起作用。

英文:

I have a JQuery script which includes the line

console.log("Killing "+selected)
$.post("http://127.0.0.1:8080/delete_birloki",{ birloki: selected})

and it logs...

Killing france

The Golang handler starts with

func deleteBirloki(rw http.ResponseWriter, req *http.Request) {
    dat,err := httputil.DumpRequest(req,true)
    if err != nil {
	    fmt.Fprintf(rw,"BAD")
	    return
    }
    fmt.Println(string(dat))

and the print out is

POST /delete_birloki HTTP/1.1
Host: 127.0.0.1:8080
Accept: */*
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Cookie: weve_been_here_before=true; _ga=GA1.1.606577919.1412775218
Origin: http://127.0.0.1:8080
Referer: http://127.0.0.1:8080/config/?birloki=&north=&east=
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36
X-Requested-With: XMLHttpRequest

birloki%5B%5D=france

Can anybody explain WHY the variable name has been changed?
in a similar case I could request for "birloki[]" with

birloki := req.PostFormValue("birloki[]")

but even that does not seem to work in this case

答案1

得分: 1

好的,以下是翻译好的内容:

好的 - 开始吧。我是个白痴。

由于 JavaScript 的可爱的无类型特性,我要发布的变量有时是字符串,有时是数组。

当它出现 [] 时,当然是一个数组 JQuery POST到Golang会损坏字段名称。

英文:

OK - shoot me. I am an idiot.

Due to the lovely untyped nature of Javascript, the variable I was posting was sometimes a string and sometimes an array.

When it appeared with [] it was, of course, an array JQuery POST到Golang会损坏字段名称。

huangapple
  • 本文由 发表于 2015年2月20日 06:37:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/28618392.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定