2

我正在尝试从我的javascript(使用jquery post)发送json到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

到底是怎么回事?

4

2 回答 2

3
$(document).ready(function() {
    $.post("/", {foo:"foo"}, function(){});
})

在 clojure 方面,您可以通过名称接收 POST 变量foo

于 2011-10-12T04:44:01.777 回答
0

我认为您的应用定义看起来有点奇怪。您正在调用 (handler/site main-routes),然后将其值用作线程宏的形式。我见过的其他路线定义看起来像

(def app 
  (-> main-routes
      wrap-base-url))
于 2011-10-12T14:40:44.280 回答