Browser says access-control-allow-origin header not present in clojure ring

为君一笑 提交于 2020-03-25 17:53:53

问题


I make a request to a server through the client browser like so https://example.com/bar, but get the error:

Access to XMLHttpRequest at 'https://example.com/bar/' from origin 'https://www.example.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

However, when using clj-http.client, I get the following headers:

{"Access-Control-Allow-Headers" "Content-Type",
  "Server" "Aleph/0.4.4",
  "Via" "1.1 vegur",
  "Content-Type" "application/edn",
  "Access-Control-Allow-Origin" "*",
  "Connection" "close",
  "Transfer-Encoding" "chunked",
  "Access-Control-Request-Method" "GET",
  "Date" "Sat, 14 Mar 2020 10:10:43 GMT",
  "Vary" "Accept-Encoding"},

If the Access-Control-Allow-Origin is present in the header through the clj-http.client request, then why does the browser say it isn't?

--- EDIT ---

What works:

;; clj-http.client gives the correct response with the headers above. 
(get "https://www.example.com/bar")

gives:

{:cached nil,
 :request-time 456,
 :repeatable? false,
 :protocol-version {:name "HTTP", :major 1, :minor 1},
 :streaming? true,
 :http-client
 #object[org.apache.http.impl.client.InternalHttpClient 0x367237a0 "org.apache.http.impl.client.InternalHttpClient@367237a0"],
 :chunked? true,
 :reason-phrase "OK",
 :headers
 {"Access-Control-Allow-Headers" "Content-Type",
  "Server" "Aleph/0.4.4",
  "Via" "1.1 vegur",
  "Content-Type" "application/edn",
  "Access-Control-Allow-Origin" "*",
  "Connection" "close",
  "Transfer-Encoding" "chunked",
  "Access-Control-Request-Method" "GET",
  "Date" "Sat, 14 Mar 2020 18:54:05 GMT",
  "Vary" "Accept-Encoding"},
 :orig-content-encoding nil,
 :status 200,
 :length -1,
 :body
 "({:foo \"bar\", :foo2 \"bar2\", :foo4 0, :foo3 [\"...
 :trace-redirects []
}

What doesn't work

;; At the client, using cljs-http.client
   (go (let [response (<! (http/get https://example.com/bar))]  
        (prn response)))

prints

{:status 0, :success false, :body "", :headers {}, :trace-redirects ["https://example.com/bar" "https://example.com/bar"], :error-code :http-error, :error-text " [0]"}

来源:https://stackoverflow.com/questions/60681757/browser-says-access-control-allow-origin-header-not-present-in-clojure-ring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!