0

我有以下代码:

defresource handle-sign-in [redirect-uri]
  :available-media-types ["application/json"]
  :allowed-methods [:post]
  :post! (prn "welcome to post")
  :post-redirect? (fn [_] ;;(ring-response
                           {:location redirect-uri}
                           ;;)
                    )
  )

当我发送请求时,我得到了错误Preflight response is not successfulXMLHttpRequest cannot load [authorize-uri] due to access control checks. 当我将重定向位置图包裹在响铃响应周围时,我没有收到错误,但我也没有在浏览器中获得重定向。我究竟做错了什么?

- 编辑 -

这是我的 system.components 配置。

(defn config []
  {:http-port  (Integer. (or (env :port) 5000))
   :middleware [[wrap-defaults api-defaults]
                wrap-with-logger
                wrap-gzip
                ignore-trailing-slash
                [wrap-reload {:dir "../../src"}]
                [wrap-trace :header :ui]
                wrap-params
                wrap-keyword-params
                wrap-cookies
                [wrap-cors #".*"]
                ]})

这就是我的新资源的样子:

(defresource handle-sign-in [authorize-uri]
  :available-media-types ["application/json"]
  :allowed-methods [:post]
  :post-redirect? true
  :as-response (fn [d ctx]
                 (-> (as-response d ctx) ;; default implementation
                     (assoc-in [:headers "Access-Control-Allow-Origin"] "*")
                     (assoc-in [:headers "Access-Control-Allow-Headers"] "Content-Type")
                     )

                 )
  :location authorize-uri
  )

但我仍然收到“请求的资源上不存在‘Access-Control-Allow-Origin’标头。” 错误。

4

1 回答 1

0

请阅读有关CORS的信息。您需要OPTIONS在资源中实现该方法或使用适当的中间件包装您的处理程序,例如ring-cors

于 2020-03-05T07:56:37.383 回答