【问题标题】:Compojure binds HTTP request params from URL, but not from a POST formCompojure 绑定来自 URL 的 HTTP 请求参数,而不是来自 POST 表单
【发布时间】:2011-04-21 05:22:16
【问题描述】:

Compojure 不绑定 POST 表单中的字段。这是我的路线定义:

(defroutes main-routes
  (POST "/query" {params :params}
    (debug (str "|" params "|"))
    "OK...")
)

当我发布一个包含字段的表单时,我得到 |{}|,即没有参数。顺便说一句,当我去http://localhost/query?param1=value1 时,params 不是空的,并且值会打印在服务器控制台上。

表单字段还有其他绑定吗?

【问题讨论】:

    标签: clojure compojure


    【解决方案1】:

    注意: (params "id") 为我返回 nil,我用 (params :id) 得到一个正确的值

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:

        确保您的输入字段具有 name="zzz" 属性,但不仅仅是 id="zzz"。

        html 表单收集所有输入并使用 name 属性发布它们

        my_post.html

        <form action="my_post_route" method="post">
            <label for="id">id</label> <input type="text" name="id" id="id" />
            <label for="aaaa">aaa</label> <input type="text" name="aaa" id="aaa" />
            <button type="submit">send</button>
        </form>
        

        my_routes.clj

        (defroutes default-handler
          ;,,,,
          (POST "/my_post_route" {params :params} 
            (str "POST id=" (params "id") " params=" params))
          ;,,,,
        

        产生类似的响应

        id=21 params={"aaa" "aoeu", "id" "21"}

        【讨论】:

          猜你喜欢
          • 2017-03-22
          • 2016-10-17
          • 2015-05-13
          • 2021-03-12
          • 1970-01-01
          • 2011-08-27
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多