0

http://frontend-erjan-vote.s3-website-us-east-1.amazonaws.com/ s3 with static website has 2 buttons, i send post request when clicking a button, it goes to be processed via api gateway backend url - https://5y7dfynd34.execute-api.us-east-1.amazonaws.com

if you press f12 on the frontend-erjan link above and click on button it gives

A cross-origin resource sharing (CORS) request was blocked because of invalid or missing response headers of the request or the associated preflight request .
To fix this issue, ensure the response to the CORS request and/or the associated preflight request are not missing headers and use valid header values.
Note that if an opaque response is sufficient, the request's mode can be set to no-cors to fetch the resource with CORS disabled; that way CORS headers are not required but the response content is inaccessible (opaque).
1 request
Request Status  Preflight Request (if problematic)  Header  Problem Invalid Value (if available)
 5y7dfynd34.execute-api.us-east-1.amazonaws.com/    blocked 
 5y7dfynd34.execute-api.us-east-1.amazonaws.com/    Access-Control-Allow-Headers    Invalid Value   
Learn more: Cross-Origin Resource Sharing (CORS)

in api gateway console in CORS section i added all things to headers i could think of:

uu

or the picture from browser:

qq

i cant see what the error is

EDIT: adding index.html where function calls the api gateway backend_url. It has no routes. I did add it before, but it did not make difference so i removed it.

this is my s3 index.html file:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>A or B?</title>
    <base href="/index.html">
    <meta name = "viewport" content = "width=device-width, initial-scale = 1.0">
    <link rel="stylesheet" href="style.css" />
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
  </head>
  <body>
    <div id="content-container">
      <div id="content-container-center">
        <h3>A or B?</h3>
        <form id="choice" name='form' method="POST" action="/">
          <button id="a" type="submit" name="vote" class="a" value="a">A</button>
          <button id="b" type="submit" name="vote" class="b" value="b">B</button>
        </form>
        <div id="tip">
          (Подсказка: вы можете голосовать несколько раз)
        </div>
      </div>
    </div>
    <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.js"></script>

    <script>
      var backend_url = "https://5y7dfynd34.execute-api.us-east-1.amazonaws.com/"

      $.ajaxSetup({
          headers: {
              'Content-Type': 'application/json',
              'Accept': 'application/json'
          }
      });

      $(document).on('click','button[name=vote]',function(){
        vote = this.value;
        $.post(backend_url, JSON.stringify({ "vote": vote }), function(result,status){
          console.log(result);
          if ("success" == status) {
            usedButton = $('button.' + vote);
            usedButton.prop('disabled', true);
            usedButton.html(vote + ' <i class="fa fa-check-circle"></i>');
            usedButton.css('opacity','0.5');
            unusedButton = $('button.' + (vote == 'a' ? 'b' : 'a'));
            unusedButton.prop('disabled', false);
            unusedButton.html(vote == 'a' ? 'b' : 'a');
            unusedButton.css('opacity','1');
            setTimeout(function(){usedButton.prop('disabled', false); usedButton.html(vote);}, 2000);
          } else {
            alert("error! :(");
          }
        });
        return false;
      });
    </script>
  </body>
</html>
ERJAN
  • 23,696
  • 23
  • 72
  • 146

1 Answers1

1

The API call is actually returning 404. Your FE is calling the url https://5y7dfynd34.execute-api.us-east-1.amazonaws.com/ without any routes. Is it intentional that it is an empty route? The URL when attempted from postman also returns a 404 which means that an empty route is not configured in the API gateway. API gateway configuration will correctly work for http status 200. So calling the right route in API gateway should resolve your CORS error also.

Aside: In your Access-Control-Allow-Origin configuration, you can configure only the Domain Names, /voting or / is not needed.

user11666461
  • 761
  • 7
  • 12
  • there are 2 routes linked to 2 backend lambdas - /voting and /result, u mean in my s3 index.html i should have backend_url + route not just the backend_url itself? i updated the question and added s3 static index.html file – ERJAN Jan 05 '23 at 05:25
  • now i attached "/voting" to var backend_url in s3 index.html - and i get 500 POST error.... – ERJAN Jan 05 '23 at 05:45
  • @ERJAN You are getting a 500 because of your integration misconfigurations. If you can give more details on your setup I may be able to help you out. – user11666461 Jan 05 '23 at 05:53
  • right now it also fails with weird 404 "Failed to load resource: the server responded with a status of 404 (Not Found)" - but there is no favicon.ico nowhere in s3 index.html file! – ERJAN Jan 05 '23 at 05:56
  • 1
    You can safely ignore favicon failures. Though it is recommended to add favicon as a best practise. Your browser is trying to add a favicon and you have not configured it. Refer this [SO post](https://stackoverflow.com/questions/39149846/why-am-i-seeing-a-404-not-found-error-failed-to-load-favicon-ico-when-not-usin) for more details. For now, you can ignore this error and concentrate on 500 coming from the API – user11666461 Jan 05 '23 at 06:01
  • that error is gone! i uploaded myicon png and added code. now only 500. the browser console points to line 42 from index.html - "$.post(backend_url, JSON.stringify({ "vote": vote }), function(result,status){" – ERJAN Jan 05 '23 at 06:11
  • i think the problem comes from "
    . "action" must be "/voting"? how does it connect and call the api method if backend url does it?
    – ERJAN Jan 05 '23 at 06:35
  • 1
    Not sure what your question is. backend_url doesn't have /voting in it. To test your application you can append it to the backend_url. The real issue was that the API was returning 500 even when testing it in Postman. Fixing that is the solution to the question in this post – user11666461 Jan 05 '23 at 07:14
  • i'm trying to make post request in a diff way - using fetch() method – ERJAN Jan 05 '23 at 07:15
  • thx, what worked is to change type of /voting route to accept ANY not just POST. i changed it in api gateway – ERJAN Jan 05 '23 at 07:26
  • 1
    @ERJAN `POST` should have worked. Changing it to `ANY` shouldn't have made any difference. Anyways, can you mark it as the answer if you feel that my suggestions helped you to get to the answer? – user11666461 Jan 05 '23 at 10:20