我正在尝试将 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)))
当我加载页面时我期望得到
got: foo from jquery
但是我得到了
got: nil from jquery
这是怎么回事?
最佳答案
$(document).ready(function() {
$.post("/", {foo:"foo"}, function(){});
})
在 clojure 端,您可以通过名称 foo
接收 POST 变量
关于jquery - 使用 jquery 发送 json 对象,但在 compojure 中接收 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7735338/