API Gateway CORS: no 'Access-Control-Allow-Origin' header API Gateway CORS: no 'Access-Control-Allow-Origin' header ajax ajax

API Gateway CORS: no 'Access-Control-Allow-Origin' header


I get the same problem. I have used 10hrs to findout.

https://serverless.com/framework/docs/providers/aws/events/apigateway/

// handler.js'use strict';module.exports.hello = function(event, context, callback) {const response = {  statusCode: 200,  headers: {    "Access-Control-Allow-Origin" : "*", // Required for CORS support to work    "Access-Control-Allow-Credentials" : true // Required for cookies, authorization headers with HTTPS   },  body: JSON.stringify({ "message": "Hello World!" })};callback(null, response);};


If anyone else is running into this still - I was able to track down the root cause in my application.

If you are running API-Gateway with custom Authorizers - API-Gateway will send a 401 or 403 back before it actually hits your server. By default - API-Gateway is NOT configured for CORS when returning 4xx from a custom authorizer.

Also - if you happen to be getting a status code of 0 or 1 from a request running through API Gateway, this is probably your issue.

To fix - in the API Gateway configuration - go to "Gateway Responses", expand "Default 4XX" and add a CORS configuration header there. i.e.

Access-Control-Allow-Origin: '*'

Make sure to re-deploy your gateway - and voila!


If you have tried everything regarding this issue to no avail, you'll end up where I did. It turns out, Amazon's existing CORS setup directions work just fine... just make sure you remember to redeploy! The CORS editing wizard, even with all its nice little green checkmarks, does not make live updates to your API. Perhaps obvious, but it stumped me for half a day.

enter image description here