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

550
Views
access to xmlhttprequest from origin http://localhost:3000 has been blocked due to CORS policy

i have set a front and a backend for an app i am developing and i want to test http requests sent from front to back while these two run in localhost.

Code sending request using React:

 async componentDidMount(){
           const {data: posts}= await axios.get("localhost:8765/...");
       }

Code for app server using Express:

const express = require('express')
const app = express()
require('./routes/...')(app)

app.listen(8765, () => {
    console.log("Server is listening on port 8765.")
})

Front end is running on localhost 3000.

This is the error i get:

Access to XMLHttpRequest at 'localhost:8765/...' from origin 'http://localhost:3000' 
has been blocked by CORS policy: Cross origin requests are only supported for protocol 
schemes: http, data, chrome, chrome-extension, chrome-untrusted, https.

i have tried various solutions i found online like:

var cors = require('cors');

app.use(cors({origin: 'http://localhost:3000'}));

or:

app.use(function (req, res, next) {

    
    res.setHeader('Access-Control-Allow-Origin', 'http://localhost:8888');

    
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');

    
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');

    
    res.setHeader('Access-Control-Allow-Credentials', true);

    
    next();
});

or:

app.use(cors())

Nothing seems to work though,i still get the same error,and i've been stuck in this for quite a while.Any ideas?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Simply use res.setHeader('Access-Control-Allow-Origin', '*');

over 4 years ago · Santiago Trujillo 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!