这可能是由于未正确设置API Gateway导致的。概括来说,您需要为API Gateway启用CORS,并且在API部署期间确保配置了正确的作用域。以下是一些示例代码:
启用CORS:
exports.handler = function(event, context, callback) { const response = { headers: { "Access-Control-Allow-Origin" : "*", // Required for CORS support to work "Access-Control-Allow-Credentials" : true // Required for cookies, authorization headers with HTTPS }, statusCode: 200, body: JSON.stringify('Hello from Lambda!'), }; callback(null, response); };
纠正API Gateway设置:
{ "swagger": "2.0", "info": { "version": "1.0.0", "title": "Example API" }, "basePath": "/v1", "schemes": [ "https" ], "produces": [ "application/json" ], "securityDefinitions": { "api_key": { "type": "apiKey", "name": "x-api-key", "in": "header" } }, "paths": { "/example": { "get": { "security": [ { "api_key": [] } ], "responses": { "200": { "description": "Response", "schema": { "$ref": "#/definitions/ExampleResponse" } } } } } }, "definitions": { "ExampleResponse": { "type": "object", "properties": { "message": { "type": "string", "example": "Hello, World!" } } } } }
此外,还要确保您正在使用正确的URL。有时,端口,路径和查询参数之类的小错误可能会导致API Gateway无法正常工作。