1 réponse
- Le plus récent
- Le plus de votes
- La plupart des commentaires
2
Your Lambda function needs to return the right headers in the response, for example:
const responseHeaders = {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
};
const body = { 'userId': 13232 };
return {
statusCode: 200,
headers: responseHeaders,
body: JSON.stringify(results),
};
You shouldn't have to configure CORS after you add those headers. But if you want to configure CORS on API Gateway then navigate to API Gateway in the console and click to open the API Gateway instance you want to change. Under details for that API Gateway open the side menu and make sure you are under resources, then you can enable CORS for a specific resource such as "/users". If you add methods then you'll need to add another header for Access-Control-Allow-Methods
such as 'Access-Control-Allow-Methods': 'GET'
.
répondu il y a 9 mois
Contenus pertinents
- demandé il y a 6 mois
- AWS OFFICIELA mis à jour il y a 3 ans
- AWS OFFICIELA mis à jour il y a 7 mois
- AWS OFFICIELA mis à jour il y a 2 ans
Amazing - thank you.
Furthermore, it worked on Postman and not in the fetch call because Postman is a developer tool. It doesn't enforce CORS by default, so your request is treated as "SAME ORIGIN". Your browser is also a developer tool so if you test your API directly in the browser, it will work just fine BUT the scripts like js are treated differently by the browser and the requests made by a script are enforced with "CROSS ORIGIN" and hence it pops a CORS error.