北城已荒凉 2015-11-04 20:13 采纳率: 0%
浏览 37

cljs-ajax POST给出403

This is my client side code. I'm using cljs-ajax for POST

(defn persist-state []
  (POST "/save" {:params {:state @state}))

Here is my server side code to handle the POST. I'm using compojure.

(POST "/save" req
      (let [state (:state (req :params))]
           (add-state! state)
           {:status 200}))

When I trigger the ajax POST it gives a 403 forbidden error. How do I get around this? Couldn't find anything online to help me.

Edit: My middleware looks like this

(def app                                                                        
  (let [handler (wrap-defaults #'routes site-defaults)]                         
    (if (env :dev) (-> handler wrap-exceptions wrap-reload) handler)))

This was generated from the lein reagent template. I'm fairly certain my problem is related to not setting anti-forgery token.

  • 写回答

1条回答 默认 最新

  • weixin_33720186 2015-11-08 02:46
    关注

    There are two basic ways to solve your problem. Which suits depends on your app and what you want.

    Option 1. If you do not want the CSRF protection, you can turn it off. To do this, you can either disable it by changing the site-defaults map or you could use api-defaults instead of site defaults (which has the csrf support disabled by default). To turn it off, you could do something like

    (let [handler (wrap-defaults #'routes (assoc-in site-defaults [security :anti-forgery] false))]  
      .....)
    

    Option 2. You need to get the server to send the current token to your client. With static forms, this is normally done by putting the token in a hidden field within the form. An alternative iuseful for dynamic content where you use javascript is to have the server generate an initial page which sets a js variable with the token in it.

    The Luminus template has quite a nice example of how you can handle these tokens in a flexible manner. It uses the Selmar package for templates and adds a new template tag that represents the csrf token.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?