From c912a9eb7acd0bc2ead5910162f69938879b6ffe Mon Sep 17 00:00:00 2001 From: Chris McCormick Date: Tue, 13 Feb 2024 11:15:01 +0000 Subject: [PATCH] Set up fetch POST/CSRF test in example. #28 --- examples/form-validation/webserver.cljs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/examples/form-validation/webserver.cljs b/examples/form-validation/webserver.cljs index 79e897c..07924f5 100644 --- a/examples/form-validation/webserver.cljs +++ b/examples/form-validation/webserver.cljs @@ -37,7 +37,14 @@ (when (aget ve "count") [:p.warning (aget ve "count" "message")]) [:p [:input {:name "_csrf" :type "hidden" :default-value csrf-token}]] - [:button {:type "submit"} "Submit"]]])) + [:button {:type "submit"} "Submit"]] + [:h3 "Fetch POST test"] + [:div#ajaxresult] + [:button#ajax "Send fetch request"] + [:script {:dangerouslySetInnerHTML + {:__html + "ajax.onclick=()=>{fetch('/ajax',{'method':'POST','data':'hello','headers':{'Content-Type':'text/plain'}}) + .then(r=>r.text()).then(d=>{ajaxresult.innerHTML=d})}"}}]])) (defn view:thank-you [] [:div @@ -83,6 +90,7 @@ (web/reset-routes app) (web/static-folder app "/css" "node_modules/minimal-stylesheet/") (.use app handle-csrf-error) + (.post app "/ajax" (fn [req res] (.send res (aget req "body")))) (.use app "/" serve-form)) (defonce serve