hello, I try to use cloudflare work embed flarum got Oops! Something went wrong during a cross-origin request. Please reload the page and try again.
how to fix it?
this is my work.js code
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request));
});
async function handleRequest(request) {
const url = new URL(request.url);
const targetUrl = `https://xxxx.com${url.pathname}${url.search}`;
let response = await fetch(targetUrl, {
headers: {
'User-Agent': 'Mozilla/5.0 (compatible; Cloudflare Workers)'
}
});
if (!response.ok) {
return new Response('Error fetching the target URL', {
status: 500
});
}
let responseText = await response.text();
responseText = responseText.replace(/(href|src)="\/([^"]*)"/g, '$1="https://xxxx.com/$2"');
return new Response(responseText, {
headers: {
'Content-Type': 'text/html',
'X-Content-Type-Options': 'nosniff',
'Referrer-Policy': 'no-referrer',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
'X-Frame-Options': 'ALLOWALL'
}
});
}
function handleOptions(request) {
return new Response(null, {
status: 204,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, Authorization'
}
});
}