首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用wai-cors正确配置CORS

使用wai-cors正确配置CORS
EN

Stack Overflow用户
提问于 2020-11-01 22:48:58
回答 1查看 105关注 0票数 0

我正在为Servant和CORS配置而苦苦挣扎:我正在通过Servant公开和API,我有以下配置:

代码语言:javascript
复制
-- Wai application initialization logic
initializeApplication :: IO Application
initializeApplication = do
  let frontCors = simpleCorsResourcePolicy { corsOrigins = Just ([pack "https://xxxx.cloudfront.net"],  True)
                                           , corsMethods = ["OPTIONS", "GET", "PUT", "POST"]
                                           , corsRequestHeaders = simpleHeaders }
  return
    $ cors (const $ Just $ frontCors)
    $ serve (Proxy @API)
    $ hoistServer (Proxy @API) toHandler server

当我通过Chromium执行这样的查询时(通过复制和粘贴):

代码语言:javascript
复制
curl 'https://api.xxx/' \
  -H 'Accept: application/json, text/plain, */*' \
  -H 'Referer: https://xxx.cloudfront.net' \
  -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36' \
  -H 'Authorization: Bearer XXX==' \
  --compressed

它可以工作,但如果我在开发控制台中复制-粘贴fetch查询:

代码语言:javascript
复制
fetch("https://api.xxx", {
  "headers": {
    "accept": "application/json, text/plain, */*",
    "authorization": "Bearer XXX=="
  },
  "referrer": "https://xxx.cloudfront.net/",
  "referrerPolicy": "no-referrer-when-downgrade",
  "body": null,
  "method": "GET",
  "mode": "cors",
  "credentials": "include"
});

我得到了:

代码语言:javascript
复制
> Access to fetch at 'https://api.xxx' from origin 'https://xxx.cloudfront.net' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
polyfills-es2015.3eb4283ca820c86b1337.js:1 GET https://api.xxx net::ERR_FAILED
e.fetch @ polyfills-es2015.3eb4283ca820c86b1337.js:1
>  (anonymous) @ VM20:1
> x:1 Uncaught (in promise) TypeError: Failed to fetch

对此有什么提示吗?尤其是为什么它可以在cUrl中运行,而不能在Chromium中运行?提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-05 05:15:36

这是一个基本的CORS问题,事实上,在corsRequestHeaders中发送没有它的Authorization会导致请求被拒绝。

我应该写下:

代码语言:javascript
复制
, corsRequestHeaders = ["Authorization", "Content-Type"]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64633184

复制
相关文章

相似问题

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