小编cur*_*cat的帖子

AWS SAM:请求的资源响应中不存在“Access-Control-Allow-Origin”标头

我正在努力让静态网站使用 CORS 调用 API 网关。我已经使用 SAM 本地运行了下面的代码,但是我在尝试从我的静态网站(本地托管)使用 jQuery 调用我的 API 时遇到以下 CORS 错误:

Failed to load http://localhost:3000/notify: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4000' is therefore not allowed access.
Run Code Online (Sandbox Code Playgroud)

我的template.yaml

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31

Globals:
  Api:
    # enable CORS; to make more specific, change the origin wildcard
    # to a particular domain name, e.g. "'www.example.com'"
    Cors: "'*'"

Parameters:
  RecaptchaSecret:
    Type: String

Resources:
  NotifierFunction:
    Type: AWS::Serverless::Function 
    Properties:
      CodeUri: notifier/build
      Handler: app.lambda_handler
      Runtime: python3.6
      Environment: 
        Variables: …
Run Code Online (Sandbox Code Playgroud)

cors aws-cloudformation aws-lambda aws-api-gateway aws-sam-cli

6
推荐指数
1
解决办法
3054
查看次数