Bre*_*kDS 4 json common-lisp hunchentoot backbone.js
我是一个javascript/web应用程序新手,并尝试使用hunchentoot和backbone.js实现我的第一个Web应用程序.我正在尝试的第一件事是理解model.fetch()和model.save()是如何工作的.
在我看来,model.fetch()会触发"GET"请求,而model.save()会触发"POST"请求.因此,我在hunchentoot中编写了一个easy-handler,如下所示:
(hunchentoot:define-easy-handler (dataset-handler :uri "/dataset") ()
(setf (hunchentoot:content-type*) "text/html")
;; get the request type, canbe :get or :post
(let ((request-type (hunchentoot:request-method hunchentoot:*request*)))
(cond ((eq request-type :get)
(dataset-update)
;; return the json boject constructed by jsown
(jsown:to-json (list :obj
(cons "length" *dataset-size*)
(cons "folder" *dataset-folder*)
(cons "list" *dataset-list*))))
((eq request-type :post)
;; have no idea on what to do here
....))))
Run Code Online (Sandbox Code Playgroud)
这旨在处理对应url为"/ dataset"的模型的获取/保存.获取工作正常,但我真的被save()搞糊涂了.我看到easy-handler触发并处理了"post"请求,但是请求似乎只有一个有意义的头,我找不到请求中隐藏的实际json对象.所以我的问题是
我在hunchentoot中尝试了"post-parameters"函数,它返回nil,并没有看到很多人通过googling使用hunchentoot + backbone.js.如果您可以引导我阅读一些有助于理解backbone.js save()工作原理的文章/博客文章,也会很有帮助.
非常感谢您的耐心等待!
感谢wvxvw的评论,我找到了这个问题的解决方案.的JSON对象可以通过调用被检索hunchentoot:raw-post-data.为了更详细,我们首先调用(hunchentoot:raw-post-data :force-text t)将后期数据作为字符串,然后将其提供给jsown:parse.完整的易处理程序如下所示:
(hunchentoot:define-easy-handler (some-handler :uri "/some") ()
(setf (hunchentoot:content-type*) "text/html")
(let ((request-type (hunchentoot:request-method hunchentoot:*request*)))
(cond ((eq request-type :get) ... );; handle get request
((eq request-type :post)
(let* ((data-string (hunchentoot:raw-post-data :force-text t))
(json-obj (jsown:parse data-string))) ;; use jsown to parse the string
.... ;; play with json-obj
data-string))))) ;; return the original post data string, so that the save() in backbone.js will be notified about the success.
Run Code Online (Sandbox Code Playgroud)
希望这能帮助那些有同样困惑的人.
| 归档时间: |
|
| 查看次数: |
1041 次 |
| 最近记录: |