AWS API 网关不存在“Access-Control-Allow-Origin"标头 [英] AWS API Gateway No 'Access-Control-Allow-Origin' header is present

查看:48
本文介绍了AWS API 网关不存在“Access-Control-Allow-Origin"标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了 API 网关的问题,我已经浏览了所有其他关于此的 SO 答案、AWS 论坛并浏览了他们的文档,但仍然没有任何乐趣.

I'm stuck on an issue with API gateway and I've gone through all the other SO answers on this, AWS forums and have been through their docs but still no joy.

我正在尝试使用 AWS API 网关设置 API,该网关调用 Lambda 函数,该函数读取/写入 DynamoDB 中的表.

I am trying to setup an API using AWS API gateway which calls a Lambda function which reads/writes to a table in DynamoDB.

DynamoDB 的 Lambda 函数正在运行.我在 AWS 中创建了一个 API,并为它创建了一个 GET 和 OPTIONS 方法.我读到 AWS 不只对 GET/POST 强制执行 OPTIONS,但是当没有 OPTIONS 方法时,我在 ajax 调用中遇到了预检错误,所以我添加了一个.

The Lambda function to DynamoDB is working. I have created an API in AWS, and created a GET and OPTIONS methods for it. I read AWS does not enforce the OPTIONS for only GET/POST but i was getting a preflight error in my ajax call when there was no OPTIONS method so I added one.

现在只是为了取得进展,我没有使用 API 密钥或授权.我可以使用 POSTMAN 成功调用我的 GET 方法,该方法返回 DynamoDB 表的内容.

For now just to make progress I am not using an API key or Authorization. I can successfully call my GET method using POSTMAN, which returns the contents of the DynamoDB table.

但是当我尝试使用 JQuery ajax 调用时,我得到了

But when i try using a JQuery ajax call i get

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

我可以看到在网络选项卡下使用 Chrome 开发工具,OPTIONS 方法返回状态 200,GET 返回状态 200,但出现上述错误.

I can see using Chrome dev tools under the network tab, the OPTIONS method returning status 200 and the GET returns status 200 but with the above error.

我尝试在 OPTIONS 和 GET 方法上启用 CORS,在每次更改后重新部署 API,尝试了以下操作 (http://enable-cors.org/server_awsapigateway.html) 但总是在控制台中得到相同的错误.

I have tried enabling CORS on both the OPTIONS and GET methods, have re-deployed the API after every change, have tried the following (http://enable-cors.org/server_awsapigateway.html) but always get the same error in the console.

我正在从桌面上的文件执行 ajax 调用,因此 origin 为空,因为该页面将作为 JS 中的单个网页应用程序部署到 S3.

I am executing the ajax call from a file on my desktop so origin is null as the page will be deployed to S3 as its a single web page application in JS.

当我在 GET 和 OPTIONS 上启用 CORS 时,我可以看到 Access-Control-Allow-Headers 是 'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token' 和 Access-Control-Allow-Origin* 是 '*'

When I enabled CORS on my GET and OPTIONS i can see that Access-Control-Allow-Headers is 'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token' and Access-Control-Allow-Origin* is '*'

我的 Ajax 调用如下所示.我还尝试复制 POSTMAN 使用的确切标头,其中设置了 Authorization 标头(我现在已在 AWS 中将其关闭),但我总是在上面遇到相同的错误

My Ajax call looks like below. I also tried copying the exact headers POSTMAN uses, which has the Authorization header set (which i have turned off in AWS for now) but i always get the same error above

var awsHeaders = {};
awsHeaders['X-Amz-Date'] = '20161127T171734';

$('#add, #cloud').click(function() {

    $.ajax({

        type: 'GET',
        headers: awsHeaders,
        dataType : "json",
        url: '...',
        success: function (res) {

            console.log('response in GET:');
            console.log(res);

        },
        error: function(data) {
            console.log('in error');
            console.log(data);
        }

    });

});

谁能解释一下我可能遗漏了什么?

Can anyone shed light on what i might be missing?

非常感谢

更新请参阅下面关于我如何根据 DigitalKapteain 评论解决此问题的答案 - 通过在来自我的 Lambda 函数的响应中设置 'Access-Control-Allow-Origin':'*' 标头.我在 AWS 文档中寻找过这个,但找不到;此链接描述了 Lambda 和 Lambda 代理之间的区别,并解释了使用 CORS 时的操作 https://serverless.com/framework/docs/providers/aws/events/apigateway/

Update See answer below regarding how I solved this as per DigitalKapteain comments - by setting the 'Access-Control-Allow-Origin':'*' header in the response from my Lambda function. I looked for this in the AWS docs but couldn;t find it. This link describes the difference between Lambda and Lambda Proxy and explains what to do when using CORS https://serverless.com/framework/docs/providers/aws/events/apigateway/

推荐答案

对 Lambda 函数的 GET 请求的响应还必须包含 Access-Control-Allow-Origin 标头.

The response for the GET request to the Lambda function must also contain the Access-Control-Allow-Originheader.

这篇关于AWS API 网关不存在“Access-Control-Allow-Origin"标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆