简体   繁体   English

如何启用CORS?

[英]How do I enable CORS?

I am unable to enable CORS on any resources from AWS Api Gateway. 我无法在AWS Api Gateway的任何资源上启用CORS。

I used the "Enable Cors" button present on the web UI: 我使用了Web UI上的“启用Cors”按钮:

在此输入图像描述

But attempting to use in development or production yields: 但试图用于开发或生产产量:

在此输入图像描述

I'm using jQuery 2.2.4 and the method $.post . 我正在使用jQuery 2.2.4和$.post方法。

What's going wrong? 出了什么问题?

UPDATE: test staging: 更新:测试升级:

在此输入图像描述

SUCCESS UPDATE: 成功更新:

AWS documentation can be quite large. AWS文档可能非常大。 What I failed to realize is that you must EXPORT a client generated SDK which has a global variable that generates methods based on the resources you provided. 我没有意识到你必须导出一个客户端生成的SDK ,它有一个全局变量,可以根据你提供的资源生成方法。 As such, I can FINALLY return a succesfull result when I use THIS code: 因此,当我使用这个代码时,我可以最终返回一个成功的结果:

  const apigClient = apigClientFactory.newClient();

    apigClient.purchaseTokenPost({}, card, {})
        .then(function(result){
            console.log(result);
        }).catch(function(result){
            console.log(result);
        });

CORS seems to be setup correctly for your method. CORS似乎是为您的方法正确设置的。 I tested with this tool: http://client.cors-api.appspot.com/client (Enter your invoke URL, the POST dropdown, and you can confirm the success "onload" callback is triggered) 我使用此工具进行了测试: http//client.cors-api.appspot.com/client (输入您的调用URL,POST下拉列表,您可以确认触发成功“onload”回调)

Can you try making your request with plain JavaScript to narrow down if it's an issue with jQuery? 如果jQuery存在问题,您是否可以尝试使用纯JavaScript来缩小范围? See: A CORS POST request works from plain javascript, but why not with jQuery? 请参阅: CORS POST请求适用于普通的javascript,但为什么不使用jQuery?

Edit: Found this on http://enable-cors.org/server_awsapigateway.html . 编辑:在http://enable-cors.org/server_awsapigateway.html上找到此信息。 Looks like the One-Click CORS button in API Gateway isn't compatible with jQuery: 看起来API Gateway中的One-Click CORS按钮与jQuery不兼容:

Amazon API Gateway adds support for CORS enabling through a simple button in the API Gateway console. Amazon API Gateway通过API网关控制台中的简单按钮添加对CORS启用的支持。 Unfortunately that button has a partial behavior, thus setting CORS correctly only for 200 answer (so not other HTTP status codes) and ignoring JQuery header support. 不幸的是,该按钮具有部分行为,因此仅为200回答(因此不是其他HTTP状态代码)正确设置CORS并忽略JQuery标头支持。 The best solution considered so far is about avoding to use the CORS button and set configurations manually. 到目前为止考虑的最佳解决方案是关于使用CORS按钮并手动设置配置。 This can be achieved in a couple of steps:... 这可以通过几个步骤实现:...

(Final) Edit: This is a bug with API Gateway not applying header mappings when the integration returns an error response. (最终)编辑:当集成返回错误响应时,这是API网关未应用标头映射的错误。 This has been a known issue for quite a while: https://forums.aws.amazon.com/thread.jspa?threadID=220324&tstart=0 这已成为一个众所周知的问题: https//forums.aws.amazon.com/thread.jspa?threadID = 220324&tstart = 0

I found that even for an 'unsecured' api call, ie one that your didn't secure with an API key (like I did to test something out), once I enabled cors it would only work if I created an API key and sent it in with the request - easy to do, may want to give it a try. 我发现,即使是一个'不安全的'api调用,即一个你没有使用API​​密钥保护的调用(就像我测试的那样),一旦我启用了cors,它只有在我创建了一个API密钥并发送时才能工作它与请求 - 容易做,可能想尝试一下。

ADDL INFO: ADDL信息:

Here is a sample jquery that worked for me after I enabled CORS on the endpoint: 这是我在端点上启用CORS后为我工作的示例jquery:

function loadData() {
            $.ajax({
                type: "GET",
                cache: false,
                url: "https://k4t999edod.execute-api.us-east-1.amazonaws.com/prod/myapicall",
                crossDomain: true,
                dataType: "json",
                headers: { 'x-api-key': 'xoeNNQ9475PCAgLtNP18cTv6YTWWB2JFfOe', 'X-Amz-Date': '1/1/2000', 'X-Amz-Security-Token': 'xoeNNQ9475PCAgLtNP18cTv6YTWWB2JFfOe' },
                success: function (response) {
                    //do something here.
                }
            });
        }

Note I included the API key in two places (I scrambled the real ones) 注意我在两个地方包含了API密钥(我对实际的密钥进行了加扰)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM