使用 jquery 发送 json 对象,但在 compojure 中接收 nil
我正在尝试将 json 从我的 javascript (使用 jquery post)发送到 compojure。我确信我做错了一些简单的事情。我的 javascript 文件(完整)看起来像:
$(document).ready(function() {
$.post("/", "foo", function(){});
});
我的 clojure 服务器看起来像:
(ns spendy.routes
(:use compojure.core
spendy.core
ring.middleware.json-params
[hiccup.middleware :only (wrap-base-url)])
(:require [compojure.route :as route]
[compojure.handler :as handler]
[compojure.response :as response]
[clj-json.core :as json]))
(defroutes main-routes
(GET "/" [] (index-page))
(POST "/" [sent-object]
(println "got:" sent-object "from jquery")
(json/generate-string (respond-to-ajax (json/parse-string (if sent-object sent-object "")))))
(route/resources "/")
(route/not-found "Page not found"))
(def app
(-> (handler/site main-routes)
(wrap-base-url)))
当我加载我期望得到的页面时
得到:来自 jquery 的 foo
但我得到了
从 jquery 得到:nil
发生了什么事?
I am trying to get send json from my javascript (using jquery post) to compojure. I am sure there is something simple that I am doing wrong. My javascript file (in it's entirety) looks like:
$(document).ready(function() {
$.post("/", "foo", function(){});
});
my clojure server looks like:
(ns spendy.routes
(:use compojure.core
spendy.core
ring.middleware.json-params
[hiccup.middleware :only (wrap-base-url)])
(:require [compojure.route :as route]
[compojure.handler :as handler]
[compojure.response :as response]
[clj-json.core :as json]))
(defroutes main-routes
(GET "/" [] (index-page))
(POST "/" [sent-object]
(println "got:" sent-object "from jquery")
(json/generate-string (respond-to-ajax (json/parse-string (if sent-object sent-object "")))))
(route/resources "/")
(route/not-found "Page not found"))
(def app
(-> (handler/site main-routes)
(wrap-base-url)))
When I load the page I expect to get
got: foo from jquery
but instead I get
got: nil from jquery
What is going on?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 clojure 端,您可以通过名称 foo 接收 POST 变量
on the clojure side you can receive the POST variable by the name of
foo
我认为你的应用程序定义看起来有点奇怪。您正在调用 (handler/site main-routes),然后使用其值作为线程宏的形式。我见过的其他路线定义看起来像
I think your app definition looks a bit odd. You're calling (handler/site main-routes), then using its value as the form for the threading macro. Other route definitions I've seen look like