当启用CORS时,API Gateway可能会出现“找不到空模型”的错误。这通常是由于API Gateway配置错误或缺少所需模型所导致的。以下是一些可能的解决方法:
示例代码:
# 在资源或方法中启用CORS
Type: AWS::ApiGateway::Method
Properties:
HttpMethod: GET
ResourceId: !Ref MyResource
RestApiId: !Ref MyRestApi
AuthorizationType: NONE
...
MethodResponses:
- StatusCode: 200
ResponseParameters:
method.response.header.Access-Control-Allow-Origin: true
ResponseModels:
application/json: EmptyModel # 确保已指定正确的模型
示例代码:
# 定义一个名为EmptyModel的模型
Resources:
EmptyModel:
Type: AWS::ApiGateway::Model
Properties:
RestApiId: !Ref MyRestApi
ContentType: application/json
Name: EmptyModel
Schema:
$schema: "http://json-schema.org/draft-04/schema#"
title: Empty Model
type: object
这样,当启用CORS时,API Gateway将能够找到所需的空模型,并处理CORS请求。请确保上述代码中的MyResource
和MyRestApi
与您的实际资源和API Gateway ID匹配。