【发布时间】:2022-01-28 18:12:40
【问题描述】:
CORS 已阻止从源“http://localhost:3000”访问“https://***.execute-api.us-east-1.amazonaws.com/dev/users”处的 XMLHttpRequest策略:对预检请求的响应未通过访问控制检查:它没有 HTTP ok 状态。
以下代码出现上述错误。
const user = await Auth.currentAuthenticatedUser();
const token = user.signInUserSession.idToken.jwtToken;
const header = {
headers: {
Authorization: token,
},
};
const resp = await API.get("OneRestApi", "/users", header);
但是从邮递员调用端点 https://***.execute-api.us-east-1.amazonaws.com/dev/users 不会导致错误。邮递员输出:
{
"success": "get call succeed!",
"url": "/users"
}
但是当我切换到另一个端点“/campaigns”时
const user = await Auth.currentAuthenticatedUser();
const token = user.signInUserSession.idToken.jwtToken;
const header = {
headers: {
Authorization: token,
},
};
const resp = await API.get("OneRestApi", "/campaigns", header);
代码运行成功。从 APIGateWay 我启用了 cors。这是“/users”路径的 lambda 代码。
/*
Copyright 2017 - 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. A copy of the License is located at
http://aws.amazon.com/apache2.0/
or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and limitations under the License.
*/
var express = require("express");
var bodyParser = require("body-parser");
var awsServerlessExpressMiddleware = require("aws-serverless-express/middleware");
// declare a new express app
var app = express();
app.use(bodyParser.json());
app.use(awsServerlessExpressMiddleware.eventContext());
// Enable CORS for all methods
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "*");
next();
});
/**********************
* Example get method *
**********************/
app.get("/users", function (req, res) {
// Add your code here
res.json({ success: "get call succeed!", url: req.url });
});
app.get("/users/*", function (req, res) {
// Add your code here
res.json({ success: "get call succeed!", url: req.url });
});
/****************************
* Example post method *
****************************/
app.post("/users", function (req, res) {
// Add your code here
res.json({ success: "post call succeed!", url: req.url, body: req.body });
});
app.post("/users/*", function (req, res) {
// Add your code here
res.json({ success: "post call succeed!", url: req.url, body: req.body });
});
/****************************
* Example put method *
****************************/
app.put("/users", function (req, res) {
// Add your code here
res.json({ success: "put call succeed!", url: req.url, body: req.body });
});
app.put("/users/*", function (req, res) {
// Add your code here
res.json({ success: "put call succeed!", url: req.url, body: req.body });
});
/****************************
* Example delete method *
****************************/
app.delete("/users", function (req, res) {
// Add your code here
res.json({ success: "delete call succeed!", url: req.url });
});
app.delete("/users/*", function (req, res) {
// Add your code here
res.json({ success: "delete call succeed!", url: req.url });
});
app.listen(3000, function () {
console.log("App started");
});
// Export the app object. When executing the application local this does nothing. However,
// to port it to AWS Lambda we will create a wrapper around that will load the app from
// this file
module.exports = app;
有趣的是,在我向 API 添加另一个路径之前,代码就可以工作了。
【问题讨论】:
标签: aws-lambda aws-api-gateway aws-amplify aws-sdk-nodejs