admin管理员组文章数量:1415698
I am calling AWS API gateway using the following code.
<!DOCTYPE html>
<html>
<head>
<script src=".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 has a POST method and it is linked with the Lambda function. I have already enabled CORS in API gatewaye. After clicking on the Request data button, showing the following error.
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.
Please help, Thanks :)
I am calling AWS API gateway using the following code.
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis./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 has a POST method and it is linked with the Lambda function. I have already enabled CORS in API gatewaye. After clicking on the Request data button, showing the following error.
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.
Please help, Thanks :)
Share Improve this question edited Nov 15, 2021 at 4:33 Ranjit Soni asked Nov 13, 2021 at 18:02 Ranjit SoniRanjit Soni 6141 gold badge8 silver badges22 bronze badges 2- I am also getting a similar problem in my project – Pallav Khare Commented Nov 14, 2021 at 6:37
- This is working fine after configuring API Gateway from scratch by enabling CORS. Thanks – Ranjit Soni Commented Nov 16, 2021 at 17:51
1 Answer
Reset to default 2I think this question is related with this not because of the API Gateway. If you configured the CORS configuration with *
you should be able to call directly from frontend.
本文标签: javascriptGetting error origin 39null39 has been blocked by CORS policyStack Overflow
版权声明:本文标题:javascript - Getting error origin 'null' has been blocked by CORS policy? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745238118a2649157.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论