I am building a web application with node js on server side and angular on client.
I am running the server on different domain and client on other domain.
My server side code:
var express = require('express');
var app = express();
var http = require("http").createServer(app);
var request = require('request');
app.use(function(req, res, next) {
console.log(req.headers);
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept, Authorization");
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
next();
});
app.get('/api/hello', function(req, res){
var data = {'message': 'Server is running'}
res.jsonp(data);
});
http.listen(5000);
and on the client side (Angular).
angular.controller('myController', function($state, $http){
$http.get('http://localhost:5000/api/hello').success(function(response, status) {
console.log(responce);
}).error( function(error, status) {
console.log(error)
});
});
My Server is running on port 5000 and my client on port 4000 on different domains.
When I send the above request from client, I get below error in browser console,
XMLHttpRequest cannot load http://localhost:5000/api/hello. Response to preflight request doesn't pass access control check: A wildcard '*' cannot be used in the 'Access-Control-Allow-Origin' header when the credentials flag is true. Origin 'http://localhost:4000' is therefore not allowed access.
I have same problem with the ionic app too.
What could be the reason behind this?
I am accessing these API's from multiple domains and from different applications like mobile and web.