Sending json object with jquery and but receiving nil in compojure
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 "/" [se开发者_运维技巧nt-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?
$(document).ready(function() {
$.post("/", {foo:"foo"}, function(){});
})
on the clojure side you can receive the POST variable by the name of foo
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
(def app
(-> main-routes
wrap-base-url))
精彩评论