Браузер говорит, что заголовок access-control-allow-origin отсутствует в кольце clojure - PullRequest
0 голосов
/ 14 марта 2020

Я делаю запрос к серверу через клиентский браузер примерно так https://example.com/bar, но получаю ошибку:

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.

Однако при использовании clj-http.client, Я получаю следующие заголовки:

{"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"},

Если Access-Control-Allow-Origin присутствует в заголовке через запрос clj-http.client, то почему браузер говорит, что это не так?

--- EDIT ---

Что работает:

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

дает:

{: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 []
}

Что не работает

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

отпечатков

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