javascript - 获取错误来源“null”已被 CORS 策略阻止?
问题描述
我正在使用以下代码调用 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。单击请求数据按钮后,显示以下错误。
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.
请帮忙,谢谢:)
解决方案
我认为这个问题与此有关,而不是因为 API Gateway。如果您配置了 CORS 配置,*
您应该可以直接从前端调用。
推荐阅读
- java - 当jsp调用servlet并重定向回同一个jsp时如何保留jsp页面上的字段值
- python - 来自 pixle 的 pong 与 CNN 和策略梯度
- reactjs - 在 Redux 中测试动作创建者时如何模拟 Firebase SDK?
- android - 尝试使用 HeapAnalytics 时出现 HeapGradleException
- coronasdk - 电晕几个transition.to失相
- scala - 收集 Akka Actor 系统指标
- google-api - Google Drive API 文件权限不足错误
- javascript - 将 chart.js 用于 Polar 图表,如何为自定义宽度创建每个可见值
- firebase - dynamic_link_first_open 事件是否也会触发 first_open?
- azure-active-directory - 无法与 AzureB2C 示例中的 WebAPI 通信