I have only been using AWS Lambdas for a few weeks, and I am trying to learn more about them specifically how to implement a Lambda I write on a webpage.
I have a project, I am working on in my local machine which is just a website, and some JavaScript with Axios. When I deploy my code to AWS Lambda using Serverless at the end of the output , I get a url, lets call it THE_URL
. When I copy and paste this page into my browser, I am brought to a web-page which has the response on it as I expect.
But on my website, I have this script being called when I press a button,
axios
.get("THE_URL")
.then(data => console.log(data))
.catch(err => console.log(err));
And this gives me the error Access to XMLHttpRequest at 'THE_URL' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Which I have looked up and it seems is fairly common. I have researched and looked into the possible solutions that there were online, and that lead me to passing in as headers, and flagging the request so it looked like this,
axios
.get("THE_URL",
{headers: {
"Access-Control-Allow-Origin": "*"
},
withCredentials: true
}
)
But with this, I then get an error which looks like this Access to XMLHttpRequest at 'THE_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.
Which I have not been able to find any solutions for online. So I figured I would ask here, and see if any of you people have ran into any errors like this?
Thanks for reading!