缺失形式参数
问题描述:
我在得到的形式参数在下面的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: {}
和我可以不知道为什么title参数不在params地图中。
我正在使用Compojure 0.6.2。
答
你有没有考虑到这一点:
随着0.6.0版本的Compojure不再添加默认中间件路线。这意味着你必须明确地将wrap-params和wrap-cookies中间件添加到你的路由中。
来源:https://github.com/weavejester/compojure
我想你的榜样与我的当前设置和它的工作。我已经包括以下内容:require [compojure.handler :as handler]
和(handler/api routes)
。
答
这是如何处理参数
(ns example2
(:use [ring.adapter.jetty :only [run-jetty]]
[compojure.core :only [defroutes GET POST]]
[ring.middleware.params :only [wrap-params]]))
(defroutes routes
(POST "/" [name] (str "Thanks " name))
(GET "/" [] "<form method='post' action='/'> What's your name? <input type='text' name='name' /><input type='submit' /></form>"))
(def app (wrap-params routes))
(run-jetty app {:port 8080})
https://github.com/heow/compojure-cookies-example
一个很好的例子,见例2下 - 中间件功能
答
这里是一个可运行的例子在2012年11月17日的:
+0
我跟着你的链接,我只看到一个GET路由,而这个问题是关于来自POST的表单参数请求。 – 2013-10-29 11:40:06
'compojure.handler'已弃用。改为使用'ring.middleware.params'。 – 2015-12-23 09:32:01
正确,并且这个答案给出了一个适用于我的示例:http://stackoverflow.com/a/8199332/3884713 – 2016-03-29 18:05:13