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

本文标签: javascriptGetting error origin 39null39 has been blocked by CORS policyStack Overflow