Trying to consume a service through a web app that I have hosted in heroku , I think the following error:
Failed to load localhost:3000/posts/1: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.
I have reviewed what is related to CORS
and I have tried two different ways
app.use(cors());
and this one
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", '*');
res.header("Access-Control-Allow-Credentials", true);
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header("Access-Control-Allow-Headers", 'Origin,X-Requested-With,Content-Type,Accept,content-type,application/json');
next();
});
I am using json-server
in my case to create a simple API for tests, since the service comes from a API which is hosted in Azure , and it came out that way, so I decided to try, and it also comes out consuming in another way.