最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Getting error origin 'null' has been blocked by CORS policy? - Stack Overflow

programmeradmin1浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 2

I 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.

发布评论

评论列表(0)

  1. 暂无评论