Compojure POST 请求中缺少表单参数

发布于 2024-11-08 05:31:14 字数 743 浏览 3 评论 0原文

我在以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你另情深 2024-11-15 05:31:14

您是否考虑过这一点:

从版本 0.6.0 开始,Compojure 不再向路由添加默认中间件。这意味着您必须显式地将wrap-params 和wrap-cookies 中间件添加到您的路由中。

资料来源: https://github.com/weavejester/compojure

我用我当前的设置尝试了你的示例,它工作了。我添加了以下内容:require [compojure.handler :as handler](handler/api paths)

Have you taken into account this:

As of version 0.6.0, Compojure no longer adds default middleware to routes. This means you must explicitly add the wrap-params and wrap-cookies middleware to your routes.

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).

不醒的梦 2024-11-15 05:31:14

这是如何处理参数的一个很好的示例

(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 - 中间件是功能

This is a great example of how to handle parameters

(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

See under Example 2 - Middleware is Features

过期以后 2024-11-15 05:31:14

您可以只给出参数列表; compojure 会自动相应地从 POST/GET 参数中获取它们。如果你需要做更复杂的事情,你可以,但我从来没有研究过如何做。例如,以下是 4clojure 的代码片段:

(POST "/problems/submit" [title tags description code]
  (create-problem title tags description code))

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:

(POST "/problems/submit" [title tags description code]
  (create-problem title tags description code))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文