请求api网关cors错误 [英] request api gateway cors error

查看:392
本文介绍了请求api网关cors错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个AWS API GATEWAY,然后按照aws doc totur步骤创建api并启用CROS和Deploy!

I created a AWS API GATEWAY,and following aws doc totur step create api and Enable CROS and Deploy !

Enable CORS

✔创建OPTIONS方法

✔ Create OPTIONS method

✔将带有空响应模型的200个方法响应添加到OPTIONS方法中

✔ Add 200 Method Response with Empty Response Model to OPTIONS method

✔将模拟集成添加到OPTIONS方法

✔ Add Mock Integration to OPTIONS method

✔将200积分响应添加到OPTIONS方法

✔ Add 200 Integration Response to OPTIONS method

✔将Access-Control-Allow-Header,Access-Control-Allow-Methods,Access-Control-Allow-Origin方法响应标题添加到OPTIONS方法

✔ Add Access-Control-Allow-Headers, Access-Control-Allow-Methods, Access-Control-Allow-Origin Method Response Headers to OPTIONS method

✔将Access-Control-Allow-Header,Access-Control-Allow-Methods,Access-Control-Allow-Origin集成响应标头映射添加到OPTIONS方法

✔ Add Access-Control-Allow-Headers, Access-Control-Allow-Methods, Access-Control-Allow-Origin Integration Response Header Mappings to OPTIONS method

✔将Access-Control-Allow-Origin方法响应标头添加到POST方法中

✔ Add Access-Control-Allow-Origin Method Response Header to POST method

✔将访问控制允许起源集成响应头映射添加到POST方法中

✔ Add Access-Control-Allow-Origin Integration Response Header Mapping to POST method

您的资源已为CORS配置.如果您在上面的结果输出中发现任何错误,请检查错误消息,并在必要时尝试通过方法编辑器"手动执行失败的步骤.

Your resource has been configured for CORS. If you see any errors in the resulting output above please check the error message and if necessary attempt to execute the failed step manually via the Method Editor.

使用此api时收到错误消息:

Get an error message when I used this api:

XMLHttpRequest无法加载 https://xxxxxxx.amazonaws.com/xxxx/xxxx .所请求的资源上没有"Access-Control-Allow-Origin"标头.因此,不允许访问来源' http://localhost:7076 .响应的HTTP状态代码为400.

XMLHttpRequest cannot load https://xxxxxxx.amazonaws.com/xxxx/xxxx. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:7076' is therefore not allowed access. The response had HTTP status code 400.

我应该完成什么步骤?

推荐答案

最后,我发现我的错误发生了什么!从AJAX发送到API网关的有效负载不是JSON格式,但是LAMBDA的输入仅支持JSON格式的数据,这导致LAMBDA无法正常处理输入.反过来,这导致LAMBDA的操作出现错误.

Finally i found what happen in my error! The payload sent from AJAX to API Gateway is not in JSON format, but LAMBDA's input supports only JSON-formatted data, which causes LAMBDA to not process the input normally. This in turn led to a mistake in LAMBDA's operation.

发生这种情况时,API GATEWAY将不会收到正常的响应,因此不会将"Access-Control-Allow-Origin"返回给客户端(AJAX),因此不会出现HTTP错误的头部.

When this happens, API GATEWAY will not receive a normal response, so it will not return 'Access-Control-Allow-Origin' to the client (AJAX), so there will be no HTTP The wrong head.

使用JSON.stringify()函数处理了有效负载后,此问题得以解决.

This problem is resolved after i've processed the payload using the JSON.stringify () function.

这篇关于请求api网关cors错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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