如何在此代码段中添加CORS?
(def app
(api
{:swagger {:ui "/docs"
:spec "/swagger.json"}}
(GET "/route-a" [] "a")
(GET "/route-b" [] "b")
(GET "/route-c" [] "c")))
我想使用https://github.com/r0man/ring-cors并尝试过此操作,但是它似乎没有任何作用。我希望看到响应标头包含Access-Control-Allow-Origin,但它丢失了。
(-> (api
{:swagger {:ui "/docs"
:spec "/swagger.json"}}
(GET "/route-a" [] "a")
(GET "/route-b" [] "b")
(GET "/route-c" [] "c"))
(wrap-cors :access-control-allow-origin #"http://localhost:81"
:access-control-allow-headers ["Origin" "X-Requested-With"
"Content-Type" "Accept"]
:access-control-allow-methods [:get :put :post :delete :options]))
答案 0 :(得分:2)
仅当请求具有与指定正则表达式匹配的Origin
头时,才会返回CORS特定的响应头(当在浏览器中使用XMLHttpRequest
进行请求时,将自动添加Origin头)
如果您尝试:
curl -vH "Origin: http://localhost:81" localhost:3000/route-a
(假设您的API在端口3000上可用),您将看到已添加了必要的响应标头。来自http://localhost:81
的AJAX请求也应该起作用。