NodeJS - Angularjs
CORS Issue.
I am having an issue with REST POST to nodeJS. REST GET seems to work fine.
I am seeing the typical message and I can't seem to fix it.
XMLHttpRequest cannot load http://test.dev:3000/api/profile. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:9000' is therefore not allowed access. The response had HTTP status code 400.
NodeJS -- I have the following set in my app.js
var cors = require('cors')
app.use(cors());
// Enables CORS
var enableCORS = function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
// intercept OPTIONS method
if ('OPTIONS' == req.method) {
// res.send(200);
res.sendStatus(200);
}
else {
next();
}
};
// enable CORS!
app.use(enableCORS);
//--------------
server log: OPTIONS /api/profile 204 1.257 ms - - POST /api/profile 400 65.141 ms - 698
NodeJS -- routes
// get email sent - Sept 12, 2015
router.post('/profile', function (req, res, next) {
console.log('api: -- start profile update -- auth server ');
console.log('api: -- parms --->> ', req.params);
console.log('api: -- body --->> ', req.body);
console.log('api: -- body.test --->> ', req.body.test);
//var updatedColumns = req.body.updatedColumns;
...
res.status(200).jsonp({ok: 'ok' });
});
Angularjs side..
service
angular.module('testApp').factory('ProfileUpdateService', ['$resource', function($resource) {
return $resource('http://test.dev:3000/api/profile');
}]);
controller
ProfileUpdateService.save('test data', function(data){
console.log('called save on ProfileUpdateService');
});