This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-12-16
Channels
- # adventofcode (24)
- # announcements (3)
- # aws (3)
- # babashka (16)
- # beginners (88)
- # biff (5)
- # calva (27)
- # cider (15)
- # cljs-dev (70)
- # clojure (87)
- # clojure-austin (3)
- # clojure-belgium (6)
- # clojure-europe (59)
- # clojure-nl (1)
- # clojure-norway (14)
- # clojure-uk (3)
- # clojurescript (37)
- # data-science (2)
- # datalevin (40)
- # datomic (1)
- # emacs (23)
- # events (2)
- # graalvm (13)
- # graphql (7)
- # gratitude (1)
- # holy-lambda (193)
- # inf-clojure (15)
- # lsp (27)
- # malli (9)
- # off-topic (20)
- # polylith (6)
- # reitit (29)
- # releases (2)
- # scittle (13)
- # shadow-cljs (51)
- # transit (15)
- # xtdb (29)
Perhaps the wrong channel to ask this, but I am having an issue using cljs-ajax + muuntaja on the server. I am leveraging cljs-ajax via re-frame’s http-xhrio. Muuntaja generates a response that looks like this:
(def response
"[\"^ \",\"~:characters/by-id\",[\"^ \",\"~uf0d87749-6f2d-43c2-b647-faa85be222f0\",[\"^ \",\"~:id\",\"~uf0d87749-6f2d-43c2-b647-faa85be222f0\",\"~:backstory\",\"\",\"~:color\",\"#737530\",\"~:portrait\",[\"^ \",\"~:gender\",\"male\",\"~:path\",\"male-trader.png\",\"~:name\",\"Male Trader\"],\"~:state\",\"new\",\"^8\",\"Aderlard the Stubborn\",\"~:voice\",[\"^ \",\"^6\",\"male\",\"^2\",\"brian\",\"~:label\",\"Male 2\",\"~:samples\",2]]],\"~:characters/all-ids\",[\"~uf0d87749-6f2d-43c2-b647-faa85be222f0\"],\"~:story\",[\"^ \",\"~:title\",null]]")
And I can decode it just fine at the repl using this:
(transit/read (transit/reader :json) response)
However, cljx-ajax is giving me a parse error. Is anyone able to point me in the right direction?My re-frame event handler looks like this:
(reg-event-fx
::create-session
(fn [cofx _]
{:http-xhrio {:method :post
:params (select-keys (:db cofx) [:characters/by-id :characters/all-ids :story])
:uri ""
:format (ajax/transit-request-format)
:response-format (ajax/transit-response-format)
:on-success [::start-session]
:on-failure [::session-error]}}))
Yeah, I'd ask it in #C03S1L9DN since it's about cljs-ajax
and not about Transit itself.
But from the error, it seems that cljs-ajax
doesn't get what Muuntaja sends - instead, it gets "[:characters/by-id ...]"
for some reason.
I'd double check what gets sent over the wire in the Network tab of the DevTools panel. If it's proper Transit, then I'd debug cljs-ajax
itself. If it's improper data, then something's probably wrong on the backend and maybe with the headers that cljs-ajax
sends.
Turns out the underlying goog.net.Xhrio
instance is stripping characters when calling .getResponseText
That is extremely weird. I myself use :http-xhrio
with Transit and I've never seen anything like it.
Actually , I am noticing the response coming back as this:
[:characters/by-id {#uuid "f0d87749-6f2d-43c2-b647-faa85be222f0" {:id #uuid "f0d87749-6f2d-43c2-b647-faa85be222f0", :backstory "", :color "#737530", :portrait {:gender "male", :path "male-trader.png", :name "Male Trader"}, :state "new", :name "Aderlard the Stubborn", :voice {:gender "male", :id "brian", :label "Male 2", :samples 2}}}][:characters/all-ids [#uuid "f0d87749-6f2d-43c2-b647-faa85be222f0"]][:story {:title nil}][:id #uuid "5b9d0c7d-d85d-4a13-996e-2c13bac671eb"]
I got it to work. Thank you. I think I expected muuntaja to format it correctly by virtue of setting the content-type
header in my route handler
Thanks again for the help. This ended up being a classic case of ordering ring middleware incorrectly. If it is useful to anyone else, the following order makes it work a treat (provided you are using similar middleware):
(defn ring-handler
"Create the main ring handler. Uses site-defaults but disables
the anti-forgery middleware."
[routes]
(-> routes
(m/wrap-params)
(m/wrap-format options)
(ring.cors/wrap-cors :access-control-allow-origin [#".*"]
:access-control-allow-methods [:get :post])
(ring.defaults/wrap-defaults defaults)))