I've created a .Net Core, AWS Lambda WebAPI. Testing locally, I ran into a CORS issue and added a CORS policy to allow origins from my domain. This worked. The app functioned properly.
I need help resolving the cors issue that came after deployment. After deployment to AWS, I'm getting the well known error:
> Access to XMLHttpRequest at 'https://myLambdaEndpoint' from origin
> 'http://myWebsite' 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.
Here's what I've done:
My request using Axios in React:
axios.post(`myEndpoint`, { FirstName, LastName, Email })
.then(res => {
if(res.errors && res.errors.length > 0) console.log(res.errors);
else
{
this.setState({loading: false, registered: true});
}
})
.catch(err => {alert(err); console.log(err)});
}
//Also tried
axios.post(`myEndpoint`, {headers: {'Access-Control-Allow-Origin': '*'}, FirstName, LastName, Email })
.then(res => {
if(res.errors && res.errors.length > 0) console.log(res.errors);
else
{
this.setState({loading: false, registered: true});
}
})
.catch(err => {alert(err); console.log(err)});
}
in Startup.cs --> Configure Services
services.addCors()
// Also tried
services.AddCors(options =>
{
options.AddPolicy("AllowOrigin",
builder =>
{
builder.WithOrigins("http://myWebsite")
.AllowAnyHeader()
.WithMethods("POST");
});
});
in the controller.cs
[EnableCors(origins: "http://myWebsite", headers: "*", methods: "post")]
my s3 bucket cors policy for the API as well as for the website
<CORSRule>
<AllowedOrigin>myWebsite</AllowedOrigin>
<AllowedMethod>POST</AllowedMethod>
<AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>
//Also tried
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedMethod>POST</AllowedMethod>
<AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>
I've tried adding triggers to the lambda function in aws as well.
Status code from the preflight is 500. Also says " Referer Policy: no-referer-when-downgrade" I'm wondering if that's the issue?
The response headers are:
> Access-Control-Request-Headers: content-type
> Access-Control-Request-Method: POST
> Origin: http://myWebsite.com
> Referer: http://myWebsite.com/
Anyone know how to fix this? I've spent hours trying things and completing searches.