Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

237
Views
How to fix cors error faced even with cors setup on the backend

I have configured my code in this manner on my express backend and have the cors library installed.

I am however getting this error:

Access to fetch at 'mybackend.com' from origin 'domain1.com' has been blocked by CORS policy: 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.

How can I solve this?

My backend and frontend code are as below respectively

const allowedDomains = [
    'domain 1',
    'domain 2',
    ];
app.use(cors(
     {
     origin: function (origin, callback) {
     if (allowedDomains.indexOf(origin) != -1) {
     callback(null, true);
     } else {
     callback(new Error(`Access has been blocked `));
     }
     },
     credentials: true,
     }
    ));
await fetch(`mybackend.com' ,{
     method:'GET',
     headers: new Headers({
     'Content-Type':'application/json',
     'Authorization':`Bearer ${token}`,
     }),
     body:null,
     })
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

As per my limited understanding of Fetch API and CORS, I think you have to add mode: 'cors' as well as the header Access-Control-Allow-Origin: '*' or Access-Control-Allow-Origin: 'mybackend.com'. After doing that, your await fetch(...) block would look something like this.

fetch(URL, {
  mode: 'cors',
  method:'GET',
  headers: {
    'Access-Control-Allow-Origin':'*',
    'Content-Type':'application/json',
    'Authorization':`Bearer ${token}`,
  },
  body:null,
})
  .then(response => response.json())
  .then(data => { ... });

Hope this helps!

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!