Can anyone with similar experience share some light?
My code as followed [published in Backend]:
import { ok, badRequest, forbidden, response } from 'wix-http-functions';
import wixData from 'wix-data';
function validate(origin) {
//I used localhost for local dev, also tried to switch to my netlify endpoint
if (origin == "http://forum.wixstudio.com" || origin == "http://localhost:4201") {
return true;
} else {
return false;
}
}
export function options_member(request) {
let headers = {
"Access-Control-Allow-Methods": "POST, GET, OPTIONS",
"Access-Control-Max-Age": "86400"
}
if (validate(request.headers.origin)) {
headers["Access-Control-Allow-Origin"] = request.headers.origin;
}
return response({ "status": 204, "headers": headers });
}
// user should only be able to get their user data
// pass in line profile ID and verify before return
// validate API key and profile ID
export async function get_member(request){
const options = {
"headers": {
"Content-Type": "application/json",
}
};
if (validate(request.headers.origin)) {
options.headers["Access-Control-Allow-Origin"] =
request.headers.origin;
options.headers["Access-Control-Allow-Headers"] = "Content-Type";
}
return wixData.query("members")
.find().then(results => {
if (results.items.length > 0) {
options.body = {
"items": results.items
}
return ok(options)
}
})
@amotor Thanks for your reply, not sure if I understood correctly, my frontend in this context in going to another code base not directly in wix - a typescript react frontend project that I am hosting in Netlify, or localhost:3000 in local environment.
Thus, my backend is deployed to wix using http-functions to expose APIs that my frontend project can call.
Are you suggesting that I moved the code to .jsw, and call the said method in http-functions.js?
Then my frontend code call this API exposed to the public in http-functions.js?
I am trying to call the API after I have deployed into netlify, current setting for origin is still a wildcard, theoretically it should, not sure why it wasn’t working - “Access-Control-Allow-Origin” : “*”
Two APIs will be called when I use options according to the velo documentations. The first one is OPTIONS, which you will get 204, note that this header has the “Access-Control-Allow-Origin” : “*” . The second API is GET, which failed, see from below