clojurescript http 请求 returns 空
clojurescript http request returns empty
我正在尝试使用 clojure/clojurescript 分别为 backend/frontend 构建一个简单的网络应用程序。我无法通过 http 请求在他们两者之间建立通信,我觉得我的问题出在前端,但我不完全确定。
这是我的后端代码:
(ns example.handler
(:require [compojure.core :refer :all]
[compojure.route :as route]
[clojure.data.json :as json]
[clj-http.client :as client]
[datomic.api :as d]
[ring.util.response :as resp]
[ring.middleware.defaults :refer [wrap-defaults site-defaults]])
(:use [hiccup.core]))
;; Route
(defroutes app-routes
(GET "/info" [] (json/write-str {:data (some-function x)})))
到目前为止一切顺利,当我转到 localhost:3000/info
时,我在浏览器上看到类似 {"data":10}
的内容,这是预期的结果。我读到使用 cljs-http 是 clojurescript 请求的标准,所以我遵循了 repo 上的简单示例,我的代码如下所示:
(ns example-cljs.core
(:require [reagent.core :as reagent :refer [atom]]
[clojure.core.async :as async]
[cljs-http.client :as http]))
(def example-req (atom nil))
(defn get-data []
(async/go
(let [response (async/<! (http/get "localhost:3000/info"))]
(reset! example-req response))))
但回应只是{:status 0, :success true, :body "", :headers {}, :trace-redirects ["localhost:3000/info" "localhost:3000/info"], :error-code :no-error, :error-text ""}
我觉得这是一个菜鸟错误,但我就是找不到它在哪里。
感谢您的帮助或提示。
所以,经过一些研究后,我找到了 this 教程,所以我将复制粘贴我发现的解决问题的内容:
在 project.clj 中将 ring-cors 依赖项添加到 hello-compojure。
:dependencies [[org.clojure/clojure "1.10.0"]
[org.clojure/tools.nrepl "0.2.13"]
[com.datomic/datomic-pro "0.9.6024"]
[org.clojure/data.json "0.2.6"]
[ring-cors "0.1.13"]
[compojure "1.6.1"]
[hiccup "1.0.5"]
[ring/ring-defaults "0.3.2"]]
在服务器 handler.clj 中需要它。
(ns hello-compojure.handler
(:require [compojure.core :refer :all]
[compojure.route :as route]
[datomic.api :as d]
[clojure.data.json :as json]
[ring.util.response :as resp]
[ring.middleware.cors :refer [wrap-cors]]
[ring.middleware.defaults :refer [wrap-defaults site-defaults]])
最后将 handler.clj 中的应用程序定义替换为:
(def app
(-> app-routes
(wrap-cors :access-control-allow-origin [#".*"]
:access-control-allow-methods [:post :get]
:access-control-allow-credentials "true"
:access-control-allow-headers "Content-Type, Accept, Authorization, Authentication, If-Match, If-None-Match, If-Modified-Since, If-Unmodified-Since")
(wrap-defaults (assoc-in site-defaults [:security :anti-forgery] false))))
我正在尝试使用 clojure/clojurescript 分别为 backend/frontend 构建一个简单的网络应用程序。我无法通过 http 请求在他们两者之间建立通信,我觉得我的问题出在前端,但我不完全确定。
这是我的后端代码:
(ns example.handler
(:require [compojure.core :refer :all]
[compojure.route :as route]
[clojure.data.json :as json]
[clj-http.client :as client]
[datomic.api :as d]
[ring.util.response :as resp]
[ring.middleware.defaults :refer [wrap-defaults site-defaults]])
(:use [hiccup.core]))
;; Route
(defroutes app-routes
(GET "/info" [] (json/write-str {:data (some-function x)})))
到目前为止一切顺利,当我转到 localhost:3000/info
时,我在浏览器上看到类似 {"data":10}
的内容,这是预期的结果。我读到使用 cljs-http 是 clojurescript 请求的标准,所以我遵循了 repo 上的简单示例,我的代码如下所示:
(ns example-cljs.core
(:require [reagent.core :as reagent :refer [atom]]
[clojure.core.async :as async]
[cljs-http.client :as http]))
(def example-req (atom nil))
(defn get-data []
(async/go
(let [response (async/<! (http/get "localhost:3000/info"))]
(reset! example-req response))))
但回应只是{:status 0, :success true, :body "", :headers {}, :trace-redirects ["localhost:3000/info" "localhost:3000/info"], :error-code :no-error, :error-text ""}
我觉得这是一个菜鸟错误,但我就是找不到它在哪里。
感谢您的帮助或提示。
所以,经过一些研究后,我找到了 this 教程,所以我将复制粘贴我发现的解决问题的内容:
在 project.clj 中将 ring-cors 依赖项添加到 hello-compojure。
:dependencies [[org.clojure/clojure "1.10.0"]
[org.clojure/tools.nrepl "0.2.13"]
[com.datomic/datomic-pro "0.9.6024"]
[org.clojure/data.json "0.2.6"]
[ring-cors "0.1.13"]
[compojure "1.6.1"]
[hiccup "1.0.5"]
[ring/ring-defaults "0.3.2"]]
在服务器 handler.clj 中需要它。
(ns hello-compojure.handler
(:require [compojure.core :refer :all]
[compojure.route :as route]
[datomic.api :as d]
[clojure.data.json :as json]
[ring.util.response :as resp]
[ring.middleware.cors :refer [wrap-cors]]
[ring.middleware.defaults :refer [wrap-defaults site-defaults]])
最后将 handler.clj 中的应用程序定义替换为:
(def app
(-> app-routes
(wrap-cors :access-control-allow-origin [#".*"]
:access-control-allow-methods [:post :get]
:access-control-allow-credentials "true"
:access-control-allow-headers "Content-Type, Accept, Authorization, Authentication, If-Match, If-None-Match, If-Modified-Since, If-Unmodified-Since")
(wrap-defaults (assoc-in site-defaults [:security :anti-forgery] false))))