Cross-Origin Request Blocked. CORS header ‘Access-Control-Allow-Origin’ missing when fetching
Answered
MD20M posted this in #help-forum
MD20MOP
So I wanted to delete a specific role from a discord user using fetch in my next.js app. But I encountered this error
My code is:
I have tried adding the header in the error but I just can't figure out how to do it correctly. Thank you in advance!
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https:// discord.com/api/v10/guilds/XXXX/members/XXXX/roles/XXXXX. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing). Status code: 403My code is:
const response = await fetch(`https://discord.com/api/v10/guilds/${guild}/members/${userId}/roles/${oldRoleId}`, {
method: "DELETE",
headers: {
Authorization: `Bot ${botToken}`,
},
});
if (!response.ok) {
throw new Error(`Failed to remove role: ${await response.text()}`);
}I have tried adding the header in the error but I just can't figure out how to do it correctly. Thank you in advance!
Answered by Clown
You can try using a server action or a route handler if you want to use it in a client component
20 Replies
Im not master at CORs. Take my words with a grain of salt.
Adding the header to the request will not work. Are you running this fetch in a server component? Also if its inside a client component then i would say you should move it into a route handler
Adding the header to the request will not work. Are you running this fetch in a server component? Also if its inside a client component then i would say you should move it into a route handler
@Clown Im not master at CORs. Take my words with a grain of salt.
Adding the header to the request will not work. Are you running this fetch in a server component? Also if its inside a client component then i would say you should move it into a route handler
MD20MOP
It's inside of a client component yeah. Should I try it in a server component?
You can try using a server action or a route handler if you want to use it in a client component
Answer
(And you cant pass it in for some reason or don't prefer doing that).
@Clown You can try using a server action or a route handler if you want to use it in a client component
MD20MOP
Thank you that worked! I have no idea how I didn't think of that
@MD20M Thank you that worked! I have no idea how I didn't think of that
Mark the answer 🙂
@MD20M Thank you that worked! I have no idea how I didn't think of that
Basset Bleu de Gascogne
could you tell me what you did please
@Basset Bleu de Gascogne could you tell me what you did please
MD20MOP
You just have to place the code which does the request in a server component
@MD20M You just have to place the code which does the request in a server component
Basset Bleu de Gascogne
But I want to access it in the client side so I can show it in the ui
@MD20M You just have to place the code which does the request in a server component
Basset Bleu de Gascogne
I will tag you in the forum I just created for it
Red-cockaded Woodpecker
if you have a back end, install the cors package, npm i cors
@Red-cockaded Woodpecker if you have a back end, install the cors package, npm i cors
Basset Bleu de Gascogne
nope front end
Red-cockaded Woodpecker
your problem comes from the requests you make, but they block them for security reasons
MD20MOP
His problem is just that you can't make a request in a client component
For anyone in the future who might need it: #having cors error while fetching from client side
Red-cockaded Woodpecker
do you want to make a discord bot without using a back end???
@Red-cockaded Woodpecker do you want to make a discord bot without using a back end???
MD20MOP
He isn't making a discord bot
And neither was I with this forum
Please make another post if its related to code help and nextjs