I've index.html that sends post request to localhost:3000/SetUser but I always get the error XMLHttpRequest cannot load https://localhost:3000/SetUser. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://localhost' is therefore not allowed access.
this is the code in my Angular controller
$http.post("https://localhost:3000/SetUser", {'a':'b'}).success(function(data, status, headers, config) {
console.log('data', data);
});
and this is the code over my server.js(nodejs) app.post
app.post('/SetUser', function(req, res) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.end();
console.log("New user added ", req.body);
});
I tried everything, not sure anymore what can be done to fix this. thanks!
*doesn't always work as expected depending on the back-end used, have you try with the address of the app instead?wildcards (*) not accepted..(dont have the exact phrase in my head right now). My guess is, your function of you server.js never runs and your headers aren't set at all to allow cors.