let me preface saying i'm still new clojurescript.
i'm trying parse api has cors turned off. choice use jsonp requests. have no idea how done. far understand jsonp, it's server returns callback script.
how implement in clojurescript? have snippet try use:
(defn get-json [url callback] (xhr/send url callback "get")) (defn logcallback [event] (let [response (.-target event)] (.log js/console (response)))) (get-json ("http://www.apiurl.com/method") logcallback) but still receive origin "localhost:3449" not allowed (cors) error. guess not jsonp request. how this?
you can use goog.net.jsonp. here's simple example:
(ns jsonp.core (:import [goog.net jsonp] [goog uri])) (defn success-handler [res] (.log js/console res)) (defn error-handler [res] (js/alert (str "found error: " res))) (let [url "http://en.wikipedia.org/w/api.php?action=opensearch&format=json&search=clojure" jsonp (goog.net.jsonp. (uri. url))] (.send jsonp nil success-handler error-handler)) if endpoint returns "wrapped payload" , param setting callback name not "callback", can pass callback param name second parameter jsonp constructor. following example gets flickr feed expects "jsoncallback" param:
(let [url "http://api.flickr.com/services/feeds/photos_public.gne?format=json" jsonp (goog.net.jsonp. (uri. url) "jsoncallback")] (.send jsonp nil success-handler error-handler)) goog.net.jsonp take care of passing param (jsoncallback in example) , unwrap payload calling function. in either case end calling success or error handler.
you can take clojurescript 101 david nolen. post 2 years ago, should still work. there's another clojurescript tutorial has similar example.
Comments
Post a Comment