By jorge
I created a function and I’m trying to trigger it from javascript. I have a browser application that makes a fetch request, but it fails with a CORS error:
Access to fetch at ‘https://<functionId>.doserverless.co/api/v1/namespaces/<namespaceId>/actions/post-transaction?blocking=true&result=true’ from origin ‘http://localhost:8000’ 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. If an opaque response serves your needs, set the request’s mode to ‘no-cors’ to fetch the resource with CORS disabled.
I can’t find documentation explaining how to configure CORS for functions. I found some pages that imply functions are associated with Apps in the app platform, but when I created my function namespace, and function they weren’t associated with any app. Not sure if that documentation is out of date? I’ve looked in settings of my function but there is no mention of CORS or how to set the Access-Control-Allow-Origin header.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
There are two ways to configure CORS for Functions.
You can add CORS headers to the object you return from your function (in the headers property). This will work for functions deployed through standalone Functions and for functions deployed as components in App Platform. When you enable your function to be a web function and invoke it via the web function URL, the body, headers, and statusCode properties will be used to determine the HTTP response that is returned to the client.
If you’re deploying your functions as components in App Platform, you can take advantage of App Platform’s built in CORS support. You can add CORS support via the UI and via the app spec file.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.