gpt4 book ai didi

amazon-web-services - 在本地运行AWS SAM CLI时启用CORS

转载 作者:行者123 更新时间:2023-12-01 01:43:51 25 4
gpt4 key购买 nike

每当我尝试通过浏览器通过POST访问无服务器lambda函数时,都会收到错误消息

Response to preflight request doesn't pass access control check: No >'Access-Control-Allow-Origin' header is present on the requested resource.



当它是 /GET时,它可以正常工作,我已经读过它,因为它没有发送飞行前请求。当我将其更改为 POST时,这是失败的。

我正在运行的命令:
sam local start-api
我的template.yaml是:
...

Resources:
PropertiesFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: target/service-0.0.1-SNAPSHOT.jar
Handler: com.aws.PropertiesHandler::handleRequest
Runtime: java8
Events:
PropertiesApi:
Type: Api
Properties:
Path: /properties
Method: post

...

如何在这些端点上启用CORS?

最佳答案

我遇到了同样的错误,并且已经在3个步骤中对其进行了修复。 (Java8中的AWS Lambda,SAM CLI v0.37.0)

  • 为标题添加选项:
  •     Map<String, String> headers = new HashMap<>();
    headers.put("Content-Type", "application/json");
    headers.put("X-Custom-Header", "application/json");
    headers.put("Access-Control-Allow-Origin", "*");
    headers.put("Access-Control-Allow-Methods", "GET, POST, OPTIONS, PUT, PATCH, DELETE");
    headers.put("Access-Control-Allow-Headers", "X-Requested-With,content-type");
  • 将Option Cors添加到template.yaml
  •     Globals:
    Function:
    Timeout: 20
    Api:
    Cors:
    AllowMethods: "'GET,POST,OPTIONS'"
    AllowHeaders: "'content-type'"
    AllowOrigin: "'*'"
    AllowCredentials: "'*'"
  • 更新template.yaml中的属性资源功能
  •           Events:
    HelloWorld:
    Type: Api
    Properties:
    # Path: /hello
    # Method: get
    Path: "/{proxy+}"
    Method: ANY

    关于amazon-web-services - 在本地运行AWS SAM CLI时启用CORS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53312412/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com