问题描述
我在获取以下 Compojure 示例中的表单参数时遇到问题:
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})
提交表单时输出总是
params: {}
我不明白为什么标题参数不在 params 映射中.
and I can't figure out why the title parameter is not in the params map.
我使用的是 Compojure 0.6.2.
I'm using Compojure 0.6.2.
推荐答案
你有没有考虑到这一点:
Have you taken into account this:
从 0.6.0 版本开始,Compojure 不再向路由添加默认中间件.这意味着您必须将 wrap-params 和 wrap-cookies 中间件显式添加到您的路由中.
来源:https://github.com/weavejester/compojure
我用我当前的设置尝试了你的例子,它奏效了.我包含了以下内容:require [compojure.handler :as handler]
和 (handler/api routes)
.
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)
.
这篇关于Compojure POST 请求中缺少表单参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!