Next.js Discord

Discord Forum

having cors error while fetching from client side

Unanswered
Basset Bleu de Gascogne posted this in #help-forum
Open in Discord
Basset Bleu de GascogneOP
The error
Access to fetch at 'https://api.minaexplorer.com/accounts/B62qqCvKnFKM3jUbPL92fZseNYA2kVSaU694GJucFScJadnTXre3Exy' from origin 'http://localhost:3001' 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.

34 Replies

Basset Bleu de GascogneOP
The code
Basset Bleu de GascogneOP
@MD20M
this is the one I am talking about
Yeah you need to have 'use server' on top not 'use client'. But the problem for you here would be that you are using some sort of a crypto wallet which from my experience require 'use client'. So either you make a new file with 'use server' and export a function from there into that file or I think you can also just add 'use server' inside a function in the same file by writing it right under the function declaration above where the function code would go
@Basset Bleu de Gascogne ya crypto wallet will require me to use client are you suggesting me to use routes ? like make a server compoenent and then try to paste the request code and from my client side request the data ?
No no just make a new file like request.tsx or something and write the request in a function in that file which you then import in the client component
And the function should just return the value
Like for example getAccountBalance.tsx
:
'use server'
export const getAccountBalance = async(accounts) =>{
        const apiUrl = `https://api.minaexplorer.com/accounts/${accounts}`;
        const response = await fetch(apiUrl, {
            method: 'GET',
            headers: {
              'Content-Type': 'application/json', // Add any headers you need
            },
          });
        if(response.ok){
            const data = await response.json();
            return(data);
        }
        else{
            console.log("Error");
        }
    }


And then you just import it in the file you sent above like:
import getAccountBalance from getAccountBalance.tsx
Basset Bleu de GascogneOP
haa got it , let me try it out
Keep me posted
@MD20M Keep me posted
Basset Bleu de GascogneOP
Sorry could not get back due to health reason's Getting this as an error
@Basset Bleu de Gascogne Sorry could not get back due to health reason's Getting this as an error
Yeah you either have to add the server component feature to the config or update next.js to v14
Basset Bleu de GascogneOP
I can update
@MD20M Yeah you either have to add the server component feature to the config or update next.js to v14
Basset Bleu de GascogneOP
add it to the config
I am checking the docs it isn't mentioned any place
@MD20M Yeah you either have to add the server component feature to the config or update next.js to v14
Basset Bleu de GascogneOP
will go with adding the server component feature to the config
@Basset Bleu de Gascogne I am checking the docs it isn't mentioned any place
Probably because it isn't experimental in v14 anymore
Basset Bleu de GascogneOP
yaa do you know what exactly I amsuppose ot add in the config
@MD20M Click to see attachment
Basset Bleu de GascogneOP
oh okay was this mentioned in the docs the link provided did not even mention this
@Basset Bleu de Gascogne oh okay was this mentioned in the docs the link provided did not even mention this
It's probably not in the docs anymore because adding it to the config isn't required anymore in the new version
I might have to see if I cna upgrade an existing next js project to 14
You can
Just change the version in package.json to v14 and do npm update
@MD20M Just change the version in package.json to v14 and do `npm update`
Basset Bleu de GascogneOP
there is a high possiblity my wallet might have issue
If the console says that when you updated just ignore it
@MD20M If the console says that when you updated just ignore it
Basset Bleu de GascogneOP
cool it works by the way thanks alot
No problem 👍
Singapura
If You want to upload image without extra traffic, You do it from client side - so there still should be solution to make requests from client without CORS problem.
Basset Bleu de GascogneOP
I have recently deployed another rpogram to vercel
and facing the cors issue
@Basset Bleu de Gascogne I have recently deployed another rpogram to vercel
Sun bear
did you solve this? facing same issue. works on local host but doesnt work on vercel