Compojure POST 请求中缺少表单参数
我在以下 Compojure 示例中获取表单参数时遇到问题:
(ns hello-world
(:use compojure.core, ring.adapter.jetty)
(:require [compojure.route :as route]))
(defn view-form []
(str "<html><head></head><body>"
"<form method=\"post\">"
"Title <input type=\"text\" name=\"title\"/>"
"<input type=\"submit\"/>"
"</form></body></html>"))
(defroutes main-routes
(GET "/" [] "Hello World")
(GET "/new" [] (view-form))
(POST "/new" {params :params} (prn "params:" params))
(route/not-found "Not Found"))
(run-jetty main-routes {:port 8088})
提交表单时,输出始终是
params: {}
,我无法弄清楚为什么标题参数不在参数映射中。
我正在使用 Compojure 0.6.2。
I'm having problems getting the form parameters in the following Compojure example:
(ns hello-world
(:use compojure.core, ring.adapter.jetty)
(:require [compojure.route :as route]))
(defn view-form []
(str "<html><head></head><body>"
"<form method=\"post\">"
"Title <input type=\"text\" name=\"title\"/>"
"<input type=\"submit\"/>"
"</form></body></html>"))
(defroutes main-routes
(GET "/" [] "Hello World")
(GET "/new" [] (view-form))
(POST "/new" {params :params} (prn "params:" params))
(route/not-found "Not Found"))
(run-jetty main-routes {:port 8088})
When submitting the form the output is always
params: {}
and I can't figure out why the title parameter is not in the params map.
I'm using Compojure 0.6.2.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您是否考虑过这一点:
资料来源: https://github.com/weavejester/compojure
我用我当前的设置尝试了你的示例,它工作了。我添加了以下内容:
require [compojure.handler :as handler]
和(handler/api paths)
。Have you taken into account this:
Source: https://github.com/weavejester/compojure
I tried your example with my current setup and it worked. I have included the following:
require [compojure.handler :as handler]
and(handler/api routes)
.这是如何处理参数的一个很好的示例
https://github.com/heow/compojure-cookies -example
请参见示例 2 - 中间件是功能
This is a great example of how to handle parameters
https://github.com/heow/compojure-cookies-example
See under Example 2 - Middleware is Features
您可以只给出参数列表; compojure 会自动相应地从 POST/GET 参数中获取它们。如果你需要做更复杂的事情,你可以,但我从来没有研究过如何做。例如,以下是 4clojure 的代码片段:
You can just give a list of parameters; compojure will automatically get them out of POST/GET params accordingly. If you need to do more complex stuff you can, but I've never looked into how. For example, here's a snippet from the code for 4clojure:
以下是截至 2012 年 11 月 17 日的可运行示例:
here is a runnable example as of 17 Nov 2012: