首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >compojure api +允许CORS

compojure api +允许CORS
EN

Stack Overflow用户
提问于 2020-09-23 07:51:22
回答 1查看 140关注 0票数 0

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

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

这就是我所拥有的:

代码语言:javascript
复制
(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自述文件中发现的一些问题。

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

代码语言:javascript
复制
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.

回应是

代码语言:javascript
复制
{: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中的打印也不会触发。

有谁能给我指个方向吗?

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2020-09-30 19:47:45

我想通了。至少有一种方法。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64019124

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档