【问题标题】:Sending json object with jquery and but receiving nil in compojure使用 jquery 发送 json 对象,但在 compojure 中接收 nil
【发布时间】:2011-12-05 19:50:32
【问题描述】:

我正在尝试从我的 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

发生了什么事?

【问题讨论】:

    标签: jquery ajax json clojure compojure


    【解决方案1】:
    $(document).ready(function() {
        $.post("/", {foo:"foo"}, function(){});
    })
    

    在 clojure 方面,您可以接收名为 foo 的 POST 变量

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-12
        • 2018-11-28
        • 2012-10-18
        相关资源
        最近更新 更多