I am trying to set my simple server to allow CORS. I am making a request localhost from a clojurescript application that I am making.
I have given a search, for example this answer: How do I add CORS to a compojure-api app?. But from what I can see, in this answer the solution was on the request and I would like a solution in the server.
Here is what I have:
(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])))
What I have was based on some questions I have found in the internet and the ring-cors README.
The error I get is in the browser's console:
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.
And the response is
{: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]"}
Nothing seems to have happened in the server. In the console, nothing changes and the print I put in the function prj/save-project
does not trigger.
Could anyone point me in the right direction?
Thx!
I figured it out. At least a way.
The problem I was having is that I was making the request with credentials. As I removed them properly I was able to make the request.