Cljs-ajax's response format

[ajax.core :as ajax :refer [GET POST PUT]]

(ajax/GET  "https://api.github.com/orgs/day8" )
;;=> #object[Object [object Object]]
>CLJS-AJAX response: {.......}

response start with “cljs-ajax response” , how can i convert it to clojurescript atom and assoc db in reagent?

The docs showed an example of getting data from the channel: https://github.com/r0man/cljs-http#async-response-handling

(go (let [response (<! (http/get "https://api.github.com/users"
                                 {:with-credentials? false
                                  :query-params {"since" 135}}))]
      (prn (:status response))
      (prn (map :login (:body response)))))

So you need to use core.async http://ku1ik.com/2015/10/12/sweet-core-async.html in this.

I remembered cljs-http has a callback form, not found in docs, feeling strange.


如果需要中文可以在 http://clojure-china.org/ 贴一个.

I think the question is about the cljs-ajax library, not cljs-http. What you need to do is install a handler:

(GET 
  "https://api.github.com/orgs/day8" 
  {:handler (fn [response] (swap! my-atom :key response))
   :error-handler (fn [{:keys [status status-text]}] (do-something))})

Note, cljs-ajax plays nicely with the promesa library if you prefer using promises instead of core.async or atom-based callbacks. You just wrap the ajax call in a new promise and then call resolve and reject in the handler and error-handler. I find the error handling to be much more predictable with promises when making simple ajax calls.

2 Likes

Thank for correcting.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.