首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Clojure http-kit: CORS设置无效

Clojure http-kit: CORS设置无效
EN

Stack Overflow用户
提问于 2019-07-01 17:21:44
回答 2查看 768关注 0票数 1

我的简单http-kit服务器:

代码语言:javascript
复制
(ns figgy.core
  (:gen-class)
  (:require [org.httpkit.server :as server]
            [compojure.core :refer :all]
            [ring.middleware.cors :refer [wrap-cors]]
            [compojure.route :as route]))
(defn fps-handler [req]
  {:status  200
   :headers {"Content-Type" "text/html"}
   :body    "Pew pew!"})

(defn mail-man []
   "{\"Spongebob Narrator\": \"5 years later...\"}")

(defn mail-handler [req]
  {:status  200
   :headers {"Content-Type" "text/json"} ;(1)
   :body    (mail-man)}) ;(2)

(defn general-handler [req]
  {:status  200
   :headers {"Content-Type" "text/html"}
   :body    "All hail General Zod!"})

(defroutes app-routes ;(3)
  (GET "/" [] fps-handler)
  (POST "/postoffice" [] mail-handler)
  (ANY "/anything-goes" [] general-handler)
  (route/not-found "You Must Be New Here")) ;(4)

(def app
  (-> app-routes
    (wrap-cors
     :access-control-allow-origin [#".*"]
     :access-control-allow-headers ["Content-Type"]
     :access-control-allow-methods [:get :put :post :delete :options])))

(defn -main
  "This is our app's entry point"
  [& args]
  (let [port (Integer/parseInt (or (System/getenv "PORT") "8080"))]
    (server/run-server #'app {:port port})
    (println (str "Running webserver at http:/127.0.0.1:" port "/"))))

每次我从我的JS应用程序打电话,我都会收到一个CORS错误,尽管我设置了wrap

CORS策略阻止从“XMLHttpRequest”从“http://localhost:8080/”访问“http://localhost:9500”:对飞行前请求的响应不通过访问控制检查:请求的资源上不存在“访问控制-允许-原产地”标头

EN

回答 2

Stack Overflow用户

发布于 2019-07-01 20:11:55

下面是我们站点中的一个工作示例,其中包括我需要传递的所有标题:

代码语言:javascript
复制
{"Access-Control-Allow-Origin" origin
 "Access-Control-Allow-Credentials" "true"
 "Access-Control-Allow-Methods" "POST, GET, OPTIONS, PUT, DELETE"
 "Access-Control-Allow-Headers" "Content-Type, Accept, Authorization, Authentication, If-Match, If-None-Match, If-Modified-Since, If-Unmodified-Since"}

我删掉了具体的应用程序。您可能需要在headders部分中包含更多内容,如X-My-Auth-Thing

票数 1
EN

Stack Overflow用户

发布于 2019-07-05 01:56:07

答案可能在于你的评论。

包-cors实际上正在工作,问题似乎是,当我在客户端代码中包含:header{:auth(basic header email密码)}}时,对后端的请求就失败了。-

这样就行了。:access-control-allow-headers ["Content-Type" "Authorization"]

顺便问一下,我想问你问题中的代码是否是应用程序中的实际代码。如果使用某种身份验证中间件,则应该在auth中间件之前添加wrap-cors。浏览器在飞行前选项请求中不包含“身份验证”标头,那么请求就会失败。首先做包装可能会解决你的问题。

浏览器开发工具也将帮助您进行调试。您可以在Chrome中的“网络”选项卡中看到飞行前请求和响应。

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

https://stackoverflow.com/questions/56839842

复制
相关文章

相似问题

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