Clojure-发出AJAX请求不返回JSON

时间:2019-07-06 14:57:00

标签: json ajax clojure ring

我有一个正在开发中的网站,后端路由不起作用,是使用compojure路由/ login进行登录,

 (POST login "/" [] check-admin-before-logging)

这是check-admin-before-logging函数的作用:

(defn redirect-admin-to-page
  []
  (-> (response/redirect "/admin/home")
      (assoc :body "logging you in...")
      (assoc :cookies {:token (token/sign-token)})))

;; function that checks whether credentials are in fact valid.
;; check-admin is a db func which compares hashed pswd with input.
(defn check-admin-before-logging-in
  [request]
  (let [username (:username (:params request))
        password (:password (:params request))]
    (if (check-admin username password)
      (redirect-admin-to-page username)
      (str "username : " username " and password " password " is not valid.\n" request))))

目前,它只是返回一个字符串,用于测试。

,对于该应用程序,我将其包装在以下中间件中:

(def app
  (-> all-routes
      (wrap-defaults (assoc-in site-defaults [:security :anti-forgery] false))
      (wrap-json-body)
      ;; (wrap-json-params)
      (wrap-json-response)
))

现在,当我在前端发出带有以下内容的AJAX请求时:

(defn submit
  []
  (let [username *grabbed from input field...*
        password *grabbed from input field...*]
     (POST "/login" {:params {:username username :password password}})))

但是当我实际单击按钮I get this时会出现问题。控制台显示发布请求本身的结果,其中:params为空,并且:body键中包含一些错误的JSON。如果我要尝试包含

(wrap-json-params) 

在应用程序内,我将在:body和:params键中得到零。此外,输出似乎有问题,因为cheshire对其进行解析有问题。如果我将ring-json库放在一边,而只对应用程序使用wrap-defaults,那么我只是将它作为body键

:body #object[org.eclipse.jetty.server.HttpInputOverHTTP 0x6ef9a9e1 HttpInputOverHTTP@6ef9a9e1]

我的问题是如何更改json,以便能够被wrap-json-params或cheshire理解?任何建议都非常感谢。

0 个答案:

没有答案