【发布时间】:2021-12-25 15:05:00
【问题描述】:
我正在使用以下代码调用 AWS API 网关。
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#btn").click(function(){
var finalJson = {
"entityType": "abc",
"entityId": "123"
};
$.ajax({
type : "POST",
contentType : "application/json",
xApiKey : "api-key"
url : "api-gateway-url",
data : JSON.stringify(finalJson),
dataType : 'json',
timeout : 100000,
success : function(data){
alert('Congrats! You got the response');
console.log(data);
},
error : function(data){
alert('Error');
console.log(data);
}
});
});
});
</script>
</head>
<body>
<h2>API Gateway call</h2>
<button type="button" id="btn">Request data</button>
<p id="demo"></p>
</body>
</html>
API Gateway 有一个 POST 方法,它与 Lambda 函数链接。 我已经在 API gatewaye 中启用了 CORS。 点击Request data按钮后,显示如下错误。
Access to XMLHttpRequest at 'API Gateway URL' from origin 'null' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
请帮忙,谢谢:)
【问题讨论】:
-
我的项目中也遇到了类似的问题
-
通过启用 CORS 从头开始配置 API 网关后,这可以正常工作。谢谢
标签: javascript jquery amazon-web-services cors aws-api-gateway