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

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

JQuery POST to Golang corrupts field names

问题

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

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

它会输出日志...

  1. Killing france

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

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

输出结果为:

  1. POST /delete_birloki HTTP/1.1
  2. Host: 127.0.0.1:8080
  3. Accept: */*
  4. Accept-Encoding: gzip, deflate
  5. Accept-Language: en-US,en;q=0.8
  6. Connection: keep-alive
  7. Content-Type: application/x-www-form-urlencoded; charset=UTF-8
  8. Cookie: weve_been_here_before=true; _ga=GA1.1.606577919.1412775218
  9. Origin: http://127.0.0.1:8080
  10. Referer: http://127.0.0.1:8080/config/?birloki=&north=&east=
  11. 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
  12. X-Requested-With: XMLHttpRequest
  13. birloki%5B%5D=france

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

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

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

英文:

I have a JQuery script which includes the line

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

and it logs...

  1. Killing france

The Golang handler starts with

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

and the print out is

  1. POST /delete_birloki HTTP/1.1
  2. Host: 127.0.0.1:8080
  3. Accept: */*
  4. Accept-Encoding: gzip, deflate
  5. Accept-Language: en-US,en;q=0.8
  6. Connection: keep-alive
  7. Content-Type: application/x-www-form-urlencoded; charset=UTF-8
  8. Cookie: weve_been_here_before=true; _ga=GA1.1.606577919.1412775218
  9. Origin: http://127.0.0.1:8080
  10. Referer: http://127.0.0.1:8080/config/?birloki=&north=&east=
  11. 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
  12. X-Requested-With: XMLHttpRequest
  13. birloki%5B%5D=france

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

  1. 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:

确定