【问题标题】:compojure api + allow CORScompojure api + 允许 CORS
【发布时间】:2021-01-09 03:12:20
【问题描述】:

我正在尝试将我的简单服务器设置为允许 CORS。我正在从我正在制作的 clojurescript 应用程序发出请求 localhost。

我已经给出了搜索,例如这个答案:How do I add CORS to a compojure-api app?。但据我所知,在这个答案中,解决方案是根据请求提供的,我希望在服务器中有一个解决方案。

这是我所拥有的:

(ns my-project.handler
  (:require [compojure.api.sweet :refer :all]
            [ring.util.http-response :refer :all]
            [ring.middleware.cors :refer [wrap-cors]]
            [punch-card-back.projects-file :as prj]
            [schema.core :as s]))

(s/defschema Any
  {s/Any s/Any})

(def app
  (-> (api
       {:swagger
        {:ui "/"
         :spec "/swagger.json"
         :data {:info {:title "My project"
                       :description "Compojure Api example"}
                :tags [{:name "api", :description "some apis"}]}}}

       (context "/api" []
         :tags ["api"]

         (GET "/get_projects" []
           :summary "Get all projects"
           (ok (prj/get-projects)))

         (POST "/save_project" []
           :body [request Any]
           :summary "Saves project into file"
           (ok (prj/save-project request)))))
      (wrap-cors :access-control-allow-origin #".*"
                 :access-control-allow-methods [:get :post])))

我所拥有的内容是基于我在互联网和ring-cors README 中找到的一些问题。

我得到的错误是在浏览器的控制台中:

Access to XMLHttpRequest at 'http://localhost:31000/api/save_project' from origin 'http://localhost:8280' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Credentials' header in the response is '' which must be 'true' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

反应是

{:status 0, :success false, :body "", :headers {}, :trace-redirects ["http://localhost:31000/api/save_project" "http://localhost:31000/api/save_project"], :error-code :http-error, :error-text " [0]"}

服务器中似乎没有发生任何事情。在控制台中,没有任何变化,我在函数prj/save-project 中的打印不会触发。

谁能指出我正确的方向?

谢谢!

【问题讨论】:

  • 请添加什么不工作:你得到什么错误或你看到什么错误行为?
  • 完成! @cfrick,我按照你的建议更新了我的问题。
  • 您提供的代码无法为我执行,在我看来#[".*"]应该是[#".*"]
  • 对不起@javahippie,确实是个错误

标签: api clojure compojure compojure-api


【解决方案1】:

我想通了。至少有办法。

我遇到的问题是我使用凭据发出请求。当我正确删除它们时,我能够提出请求。

【讨论】:

    猜你喜欢
    • 2019-09-08
    • 2014-04-21
    • 2017-04-09
    • 2021-11-27
    • 1970-01-01
    • 1970-01-01
    • 2021-11-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多