jquery - 使用 jquery 发送 json 对象,但在 compojure 中接收 nil

标签 jquery ajax json clojure compojure

我正在尝试将 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/

相关文章:

javascript - 从数组中的值查找 DOM 元素

javascript - 从 sql footable 重新加载数据

python - 如何从 Android 应用程序连接到 Odoo 数据库

iphone - 在 cocoa 中使用 JSON

jquery - 在函数外部访问 jQuery 脚本

javascript - 我可以使用 JS 一次使用 JSON 数组中的一个字符串吗?

javascript - 混契约(Contract)步和异步 javascript/jquery 并在最后获得成功函数

javascript - 当我使用 Javascript 时,我的 HTML 内容不显示

javascript - 如何绑定(bind) Jquery UI 工具提示中动态生成的元素

json - ExtJS 网格 - 如何关注添加的行?