探索ClojureScript AJAX库 - `cljs-ajax`: 简洁而强大的Web交互工具
(ns example.core
(:require [cljs.nodejs :as nodejs]
[cljs-ajax.core :refer [GET POST]]))
(nodejs/enable-util-print!)
(defn on-success [response]
(println "成功: " response))
(defn on-failure [response]
(println "失败: " response))
(defn fetch-data []
(GET "/data" {:params {:foo "bar"}
:handler on-success
:error-handler on-failure}))
(defn post-data []
(POST "/submit" {:params {:user "username" :pass "password"}
:handler on-success
:error-handler on-failure}))
(defn -main []
(fetch-data)
(post-data))
这段代码展示了如何在ClojureScript中使用cljs-ajax
库来发送GET和POST请求。首先,我们定义了成功和失败时的回调函数,然后定义了获取数据和提交数据的函数,最后在主函数中调用这些函数。这个例子简洁而直接,适合作为cljs-ajax
库使用的入门示例。
评论已关闭