2011-10-12 47 views
3

我想從我的JavaScript(使用jquery post)發送json到compojure。我確信有一些事情很簡單,我做錯了。我的javascript文件(在它的全部)是這樣的:發送與jQuery的JSON對象,但在組合中收到零

$(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))) 

當我加載頁面我希望得到

了:FOO從jquery

但是我得到了

了:從jQuery的

零發生了什麼事?

回答

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

在Clojure的方面,你可以通過foo

0

名稱收到POST變量我想你的應用程序定義看起來有點奇怪。你正在調用(handler/site main-routes),然後使用它的值作爲線程宏的表單。我見過的其他路線定義看起來像

(def app 
    (-> main-routes 
     wrap-base-url))