Ranjit Soni
Ranjit Soni

Reputation: 614

Getting error origin 'null' has been blocked by CORS policy?

I am calling AWS API gateway using the following code.

<!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 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 :)

Upvotes: 2

Views: 11852

Answers (1)

gkocyigit
gkocyigit

Reputation: 65

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.

Upvotes: 2

Related Questions