Fork me on GitHub
#re-frame
<
2020-05-09
>
Spaceman11:05:23

I have the following effect in my reframe app that sends a get request:

(re-frame.core/reg-event-fx
  :browse-products
  (clojure.core/fn
   [{:keys [db]} [___51233__auto__ location]]
   (prn "browsing products")
   {:http-xhrio
    {:method :get,
     :uri "/fetch-something",
     :timeout 8000,
     :params {:location location},
     :on-success [:success],
     :on-failure [:fail],
     :response-format (ajax/json-response-format {:keywords? true})},
    :db (assoc db :user-location (:description location))}))
And in the server side I print the response before sending it and seems to be okay:
{:status 200, :headers {"Content-Type" "application/edn", "Access-Control-Allow-Headers" "Content-Type", "Access-Control-Allow-Origin" "", "Access-Control-Request-Method" "GET, OPTIONS", "Access-Control-Allow-Credentials" true}, :body "[{:foo ...

But for some reason the on-failure event is being triggered. How do I identify the problem? How to fix this?

Spaceman12:05:13

printing the response on failure gives the following @mikethompson , where original-text is the right body

{:response nil, :last-method "GET", :original-text "[{:foo...

mikethompson12:05:44

And the status?

Spaceman12:05:53

@mikethompson status message is: Unexpected token : in JSON at position 2 Format should have been JSON keywordize

Spaceman12:05:48

what does this mean?

Spaceman12:05:00

Seems like there's a problem with parsing, but why would that be? Do I need to change the response-format?

Spaceman12:05:10

status is 200 though

mikethompson12:05:27

Hard to say. You'll have to debug. Maybe add:

:format          (ajax/json-request-format)
to the effect

Spaceman14:05:04

I wouldn't expect format to work, and it doesn't in practice too

Spaceman14:05:07

how do I debug? I told you the error message.

p-himik14:05:00

You send it EDN but ask to parse it as JSON. Just read the error message and see the payload that the client receives.

Spaceman14:05:59

what would the correct response format be then?

Spaceman14:05:17

using :detect gives ["keywords are not allowed as response formats in ajax calls: " :detect]

p-himik14:05:44

What made you think that :detect is a valid format?

Spaceman14:05:19

a table in the documentation

p-himik14:05:38

Apart from the table, there's other information. Literally the first sentence tells you what is expected of a format. Don't try blindly every visible option - just read the documentation. It's all there.

Spaceman14:05:14

what's tripping me is that json-response-format did work for a different request. The only difference was that that was returning an edn map and not an edn vector, so I would expect this to work too

p-himik14:05:49

An empty EDN map is a valid JSON object. I cannot see any other way it could work though.

Spaceman14:05:53

it works though for sure. In any case, I tried edn-response-format for this one. It stopped going for on-faliure, but in the on-success handler, this is what comes in as the parameter when printed: #ajax.interceptors.ResponseFormat{:read #object[ajax$edn$edn_read], :description "EDN", :content-type ["application/edn"]}

Spaceman14:05:56

Where's the body?

p-himik17:05:16

What you have printed is the result of (edn-respose-format) and not the response itself.

Spaceman20:05:32

Yeah it works. I wasn’t wrapping that in parentheses. Okay so a strange thing is happening. Using edn-response-format for the response that I was using json-response-format {:keywords? true} for doesn’t work.