【发布时间】:2020-07-31 00:16:36
【问题描述】:
我遇到了一个问题,POST 端点在 Postman 中运行时返回响应,但在浏览器中运行时却没有。
我已经通过无服务器在 AWS 上设置了一个 API 端点。这是 .yml 的配置:
service: tableau-export-rest
provider:
name: aws
runtime: nodejs10.x
region: eu-west-1
stage: ${opt:stage, 'dev'}
timeout: 900
memorySize: 3008
functions:
storeExportFiters:
handler: index.storeExportFiters
events:
- http:
path: /store-export-filters
method: post
cors: true
端点解析器storeExportFiters(它是一个 lambda)现在只返回一条成功消息:
module.exports = (event, ctx, cb) => {
return cb(null, {
statusCode: 200,
body: JSON.stringify({
worked: true
})
});
}
当我将它部署到 AWS 并尝试通过 POST 请求从 Postman 访问端点时,没有正文或任何内容,它会向我发送响应。当我尝试在浏览器中执行此操作时,我收到 cors 错误:
访问 XMLHttpRequest 在 'https://myapi.com/store-export-filters' 来自原点 'http://localhost:9003' 已被 CORS 阻止 政策:没有“Access-Control-Allow-Origin”标头出现在 请求的资源。
这是用于尝试从端点获取响应的浏览器代码。我使用Axios 进行http 请求:
axios.post('https://myapi.com/store-export-filters')
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
我不明白为什么我会在这里收到 CORS 错误,尤其是当它在我的机器上的 Postman 中工作时。
【问题讨论】:
标签: javascript ajax