Hey all,
I’m trying to access a function in http-functions from the frontend using fetch(). I’ve tried with wix-fetch and the default fetch. I can access the function just fine when I’m not using a premium domain and I navigate to .wixstudio.io in the browser. However, when I publish to .com I get this CORS error:
Access to fetch at ‘<“mysite”>.com/_functions/<“myfunction”>’ from origin ‘https://www.<“mysite”>.com’ 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.
Any help would be super awesome!
Maybe you have first to elevate? Not sure. But sounds not normal.
https://dev.wix.com/docs/velo/api-reference/wix-auth/elev
Or you could try the alternative way for Wix-Fetch and use AXIOS.
This for you will need to install AXIOS-NPM.
Try it out and let us know, what was the issue at the end.
I figured out the issue:
For each function in http-functions.js I added another function that handled the preflight request:
export function options_<my_function_name>(request) {
let headers = {
“Access-Control-Allow-Origin”: “<my_origin>”,
“Access-Control-Allow-Headers”: “”
};
return response({ status: 204, headers: headers });
}
The above would match up with the function below, handling preflight request:
export function post_<my_function_name>(request) {
…
}
1 Like