Answers for "m origin 'http://localhost:3000' has been blocked by cors policy: no 'access-control-allow-origin' header is present on the requested resource."

0

'http://localhost:3000/api/posts' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource in Angular

just add below code to you app.js and your all the error are gone thank me later 

app.use((req, res, next) => {
  res.setHeader("Access-Control-Allow-Origin", "*");
  res.setHeader(
    "Access-Control-Allow-Headers",
    "Origin,X-Requested-With,Content-Type,Accept"
  );
  res.setHeader(
    "Access-Control-Allow-Methods",
    "GET,POST,PUT,PATCH,DELETE,OPTIONS"
  );
  next();
});
Posted by: Guest on March-11-2022
32

Access to XMLHttpRequest at 'http://localhost:5000/mlphoto' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

var express = require('express')
var cors = require('cors')  //use this
var app = express()

app.use(cors()) //and this

app.get('/user/:id', function (req, res, next) {
  res.json({user: 'CORS enabled'})
})

app.listen(5000, function () {
  console.log('CORS-enabled web server listening on port 5000')
})
Posted by: Guest on November-13-2020
6

Access to XMLHttpRequest at from origin HTTP localhost:3000 has been blocked by CORS policy

const cors = require('cors');
const corsOptions ={
    origin:'http://localhost:3000', 
    credentials:true,            //access-control-allow-credentials:true
    optionSuccessStatus:200
}
app.use(cors(corsOptions));
Posted by: Guest on March-19-2021

Code answers related to "m origin 'http://localhost:3000' has been blocked by cors policy: no 'access-control-allow-origin' header is present on the requested resource."

Code answers related to "Javascript"

Browse Popular Code Answers by Language