Last active
July 16, 2016 15:14
-
-
Save MoyTW/8589fd82cc7a45febba8 to your computer and use it in GitHub Desktop.
Some basic examples of how to use XhrIo. Server is at: https://github.com/MoyTW/BlogSnippets/tree/master/XhrIoExamples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns xhrio-examples.examples | |
(:require [goog.net.XhrIo :as xhrio] | |
[goog.structs :as structs] | |
[goog.Uri.QueryData :as query])) | |
(def get-text-url "http://localhost:3000/get-text") | |
(def get-json-url "http://localhost:3000/get-json") | |
(def post-url "http://localhost:3000") | |
(def post-form-url "http://localhost:3000/post-form") | |
(def post-json-url "http://localhost:3000/post-json") | |
;;;; The simplest possible request: | |
;;; | |
;;; Signature: (send url callback) | |
;;; * url = target URL of the request, either a string or goog.Uri instance. | |
;;; * callback = the function to be called when the request completes. | |
;;; | |
;;; Note that it will default to Http GET if no method is specified. | |
(xhrio/send | |
get-text-url ; url | |
#(js/alert ; callback | |
(str "From " get-text-url " /w GET received: " | |
(.getResponseText (.-target %))))) | |
;;;; Requesting a JSON response: | |
;;; | |
;;; Instead of getResponseText, we use getResponseJson. However, if you have | |
;;; a need to get the raw text, you can still use getResponseText to get the | |
;;; contents as a string. | |
(xhrio/send | |
get-json-url ;url | |
#(js/alert ; callback | |
(str "From " get-json-url " /w GET received: " | |
(js->clj (.getResponseJson (.-target %)) | |
:keywordize-keys true)))) | |
;;;; POSTing form data: | |
;;; | |
;;; Signature (send url callback method contents) | |
;;; * method = Http method, as a string (so, "GET" or "POST" or "PUT" etc) | |
;;; * contents = The contents of the request, in string form. An easy way to | |
;;; convert a map into the appropriate form is to use a goog.Uri.QueryData | |
;;; object and convert it to a string. Form is the standard format for | |
;;; application/x-www-form-urlencoded. | |
;;; | |
;;; Note that POST requests default to application/x-www-form-urlencoded. If | |
;;; you want to make POST requests with other types, see below: | |
(def post-form-contents | |
(->> {:make "Mazda" :year 1998} ; contents | |
(clj->js) | |
(structs/Map.) | |
(query/createFromMap) | |
(.toString))) | |
(xhrio/send | |
post-form-url ; url | |
#(js/alert ; callback | |
(str "From " post-form-url " /w POST:" | |
"\nPosted: " post-form-contents | |
"\nReceived: " (.getResponseText (.-target %)))) | |
"POST" ; method | |
post-form-contents) ;contents | |
;;;; POSTing json (or other types) | |
;;; | |
;;; Signature: (send url callback method contents headers) | |
;;; * contents = the body of the request, formatted as appropriate | |
;;; * headers = headers, as a JavaScript Object or goog.structs.Map | |
(def post-json-contents | |
(JSON/stringify (clj->js {:title "A New Hope" :date "2014-05-17"}))) | |
(def post-json-headers | |
(structs/Map. (clj->js {:Content-Type "application/json"}))) | |
(xhrio/send | |
post-json-url ; url | |
#(js/alert ; callback | |
(str "From " post-json-url " /w POST:" | |
"\nPosted: " post-json-contents | |
"\nReceived: " (.getResponseText (.-target %)))) | |
"POST" ; method | |
post-json-contents ;contents | |
post-json-headers) ; headers | |
;;;; Request /w Timeout | |
;;; | |
;;; Signature: (send url callback method contents headers timeout) | |
;;; * timeout = timeout interval, in milliseconds. 0 defers to browser. | |
(xhrio/send | |
post-json-url ; url | |
#(js/alert ; callback | |
(str "Timeout of 1ms, success? " | |
(.isSuccess (.-target %)))) | |
"POST" ; method | |
post-json-contents ; contents | |
post-json-headers ; headers | |
1) ; timeout |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment