在Compojure POST请求中缺少表单参数 [英] Missing form parameters in Compojure POST request

查看:77
本文介绍了在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\>
type = \text\name = \title\/>
< input type = \submit\/>
/ form>< / body>< / html>))

(defroutes main-routes
(GET/[]Hello World)
GET/ new[](view-form))
(POST/ new{params:params}(prnparams:params))
(route / not-found ))

(run-jetty main-routes {:port 8088})

提交表单时,输出总是

  params:{} 

p>

,我不知道为什么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) / p>

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.

解决方案

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

这篇关于在Compojure POST请求中缺少表单参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆