我正在使用AngularJS资源基本上是$ .ajax()一些表单数据。
Post.put({user:$scope.getLoggedInUser(), action:"new"},{body:$scope.postBody, "public":$scope.postPublic}, function(post) {
表单上有一个名为“public”的复选框。
我正在使用此函数将表单数据与URL中的参数合并:
(defn get-params [request]
(merge (:params request) (parse-string (slurp (request :body)) true)))
当我从路线的处理程序中打印出来时,如果选中该复选框,我会得到一个很好的true
:
(println (:public (get-params request)))
然而,当我将参数(即不是println)传递给我的控制器中与数据库对话的另一个函数时,我在该函数的开头再做一个println并改为nil
。
我已尝试将其作为
传递(read-string x)
(boolean (Boolean/valueOf x))
(Boolean/valueOf x)
无济于事。
可能导致它的一件事(但我不知道为什么)是我通过身份验证包装请求:
(auth? request #(create x y z))
(create)
函数在数据库中创建记录。
无论我尝试过什么,我都无法成为true
。
编辑:更完整的代码
CONTROLLER
(defn auth? [request callback-true & [callback-false]]
(println (callback-true))
(let [login-response (auth/login request)]
(if (and (not (string? login-response))
login-response)
(callback-true)
(if callback-false
(callback-false)
(json-response (str "{\"auth\":" login-response "}"), 401)))))
(defn create [user logged-in-user body public?]
(if (= logged-in-user user)
(json-response (post-view/single-post (post/create user body public?)))
(json-response "{\"auth\":\"can't create post under different logged in user\"}" 401)))
(defroutes routes
....
(PUT "/api/:user/new" request
(println request)
(auth? request
#(create (:user (request :params))
(:user (request :session))
(:body (get-params request))
(:public (get-params request)))))
....
)
MODEL
(defn create [username body public?]
(println public?)
(when-not (and
(str/blank? body)
(str/blank? username))
(let [user-id (:id (get-id-from-username username))
new-post-id
(:id
(sql/with-connection db
(sql/insert-values :post
[:usr_id :body :public] [user-id body (Boolean/valueOf public?)])))]
(by-id new-post-id))))
答案 0 :(得分:0)
请勿两次致电get-params
。 (request:body)返回一个无法读取两次的流。