我像这样使用Ajax请求传递表单数据,其中(:bar db)
为{:foo1 "bar1" :foo2 "bar2"}
(let [form-data (doto
(js/FormData.)
(.append "foo" (:bar db)))]
(ajax-request
{:uri "http://localhost:5000/foo/bar"
:method :post
:body form-data
:handler #(prn "handling response")
:response-format (edn-response-format)
:format (edn-request-format)})
)
但是在服务器端,当我从请求中提取主体时,得到以下信息:
"------WebKitFormBoundary1sufyH7YYiLHE9oZ\r\nContent-Disposition: form-data; name=\"foo\"\r\n\r\n{:foo1 \"bar1\", :foo2 \"bar2\"}\r\n------WebKitFormBoundary1sufyH7YYiLHE9oZ--\r\n"
一个字符串,而不是预期的映射{:foo1 "bar1" :foo2 "bar2"}
。如何将表单数据转换为地图?
https://github.com/JulianBirch/cljs-ajax#getpostput
指定:request-format并使用:body是cljs-ajax方法的不正确用法。如果指定:body,则请求格式本质上是原始表单数据。您需要原始表格数据吗?我怀疑您真正想要的是:
(let [params {"foo" (:bar db)}]
(ajax-request
{:uri "http://localhost:5000/foo/bar"
:method :post
:params params
:handler #(prn "handling response")
:response-format (edn-response-format)
:format (edn-request-format)})
)