<legend id='OIcIm'><style id='OIcIm'><dir id='OIcIm'><q id='OIcIm'></q></dir></style></legend>
      1. <i id='OIcIm'><tr id='OIcIm'><dt id='OIcIm'><q id='OIcIm'><span id='OIcIm'><b id='OIcIm'><form id='OIcIm'><ins id='OIcIm'></ins><ul id='OIcIm'></ul><sub id='OIcIm'></sub></form><legend id='OIcIm'></legend><bdo id='OIcIm'><pre id='OIcIm'><center id='OIcIm'></center></pre></bdo></b><th id='OIcIm'></th></span></q></dt></tr></i><div id='OIcIm'><tfoot id='OIcIm'></tfoot><dl id='OIcIm'><fieldset id='OIcIm'></fieldset></dl></div>
        • <bdo id='OIcIm'></bdo><ul id='OIcIm'></ul>
      2. <small id='OIcIm'></small><noframes id='OIcIm'>

      3. <tfoot id='OIcIm'></tfoot>

        带有 AWS cognito 的无服务器框架生成 CORS 错误

        时间:2023-07-06
        • <legend id='gpngm'><style id='gpngm'><dir id='gpngm'><q id='gpngm'></q></dir></style></legend>

          <tfoot id='gpngm'></tfoot>

          <small id='gpngm'></small><noframes id='gpngm'>

                  <bdo id='gpngm'></bdo><ul id='gpngm'></ul>
                    <tbody id='gpngm'></tbody>
                • <i id='gpngm'><tr id='gpngm'><dt id='gpngm'><q id='gpngm'><span id='gpngm'><b id='gpngm'><form id='gpngm'><ins id='gpngm'></ins><ul id='gpngm'></ul><sub id='gpngm'></sub></form><legend id='gpngm'></legend><bdo id='gpngm'><pre id='gpngm'><center id='gpngm'></center></pre></bdo></b><th id='gpngm'></th></span></q></dt></tr></i><div id='gpngm'><tfoot id='gpngm'></tfoot><dl id='gpngm'><fieldset id='gpngm'></fieldset></dl></div>
                  本文介绍了带有 AWS cognito 的无服务器框架生成 CORS 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我从 Angular 前端收到此错误消息,但我无权触摸我的 lambda 代码:

                  I get this error message from the Angular frontend and I am not authorized to touch my lambda code:

                  `Access to fetch at 'https://testapicd.***.***.com/localization/v1/role' from origin 'https://localization.test.***.***.com' has been blocked by CORS policy: 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.`
                  

                  我到处查看,我的代码中似乎没有错误.我的无服务器代码是

                  I have looked everywhere and there seems to be no bug in my code. My serverless code is

                    getrole:
                      handler: v1/handler_get_role.get_role
                      name: get_role
                      events:
                        - http:
                            path: v1/role
                            method: get
                            cors: true
                            authorizer:
                              name: CognitoCSAuthorizer
                              type: COGNITO_USER_POOLS
                              arn: ${file(config.${self:provider.stage}.json):userpoolarn}
                  

                  我已经对所有设置进行了三次检查,一切似乎都正确.有什么建议该怎么做吗?该功能在开发环境中有效,但在我将其部署到测试环境时无效.

                  I have triple-checked all the settings and everything seems correct. Any advice what to do? The functionality works in the dev environment but not when I deploy it to the test environment.

                  如果我直接针对 API 尝试使用令牌,那么它也不起作用(但在开发中工作正常).我什至不再相信这是一个 CORS 问题.我认为 jwt 令牌是错误的.

                  If I try the token directly against the API, then it does not work either (but worked fine in dev). I don't even believe anymore that it is a CORS problem. I think that the jwt token is wrong.

                  def get_role(event, context):
                      return {
                          'statusCode': 200,
                          'headers': {
                           'Content-Type': 'application/json',
                           'Access-Control-Allow-Origin' : '*', # Required for CORS support to work
                           'Access-Control-Allow-Credentials': 'true',
                          },
                          'body': json.dumps("TEST")
                       }
                  

                  推荐答案

                  我已经为这个问题苦苦挣扎了好几个小时(如果不是几天),结果发现我不仅必须在 serverless.yml 上启用 cors 文件,但还将响应标头作为属性添加到您从 Lambda 返回的对象中.

                  I have wrestled with this problem for hours (if not days) before and it turned out not only I had to enable cors on the serverless.yml file but also add the response headers as attributes in the object you return from your Lambda.

                  应该这样做:

                  const response = {
                      statusCode: 200,
                      headers: {
                        'Access-Control-Allow-Origin': '*',
                        'Access-Control-Allow-Credentials': true,
                      },
                      body: JSON.stringify({
                        product: product
                      }),
                    };
                  

                  这篇文章当时救了我的命,我希望它拯救你的!

                  This article saved my life back then and I hope it saves yours!

                  这篇关于带有 AWS cognito 的无服务器框架生成 CORS 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何在 Django REST 框架上启用 CORS 下一篇:如何解决我的 Django API 的 CORS 问题?

                  相关文章

                • <small id='BudjV'></small><noframes id='BudjV'>

                • <i id='BudjV'><tr id='BudjV'><dt id='BudjV'><q id='BudjV'><span id='BudjV'><b id='BudjV'><form id='BudjV'><ins id='BudjV'></ins><ul id='BudjV'></ul><sub id='BudjV'></sub></form><legend id='BudjV'></legend><bdo id='BudjV'><pre id='BudjV'><center id='BudjV'></center></pre></bdo></b><th id='BudjV'></th></span></q></dt></tr></i><div id='BudjV'><tfoot id='BudjV'></tfoot><dl id='BudjV'><fieldset id='BudjV'></fieldset></dl></div>
                  <legend id='BudjV'><style id='BudjV'><dir id='BudjV'><q id='BudjV'></q></dir></style></legend>
                  <tfoot id='BudjV'></tfoot>

                      • <bdo id='BudjV'></bdo><ul id='BudjV'></ul>