2
的NodeJS - AngularjsNodejs http://test.dev:3000/api/profile。没有 '访问控制允许来源' 头产地的 'http://本地主机:9000'
CORS问题。
我在使用REST POST到nodeJS时遇到问题。 REST GET似乎工作正常。
我看到了典型的消息,我似乎无法修复它。
XMLHttpRequest无法加载http://test.dev:3000/api/profile。请求的资源上没有“Access-Control-Allow-Origin”标题。因此不允许访问原产地'http://localhost:9000'。响应有HTTP状态代码400
的NodeJS - 我有以下我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);
//--------------
服务器日志: 选项/ API /简档204 1.257毫秒 - - POST/API /简档400 65.141毫秒 - 698
的NodeJS - 航线
// 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侧..
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');
});
为什么你们都使用cors模块并手动实现自己的CORS支持?你不应该做一个或另一个吗?我猜这两个实现可能会互相混淆。特别是你自己对OPTIONS的支持可能会搞乱你正在使用的CORS模块。 – jfriend00
我已经拿出一个然后测试。删除了其他测试。同样的问题。 – philipfwilson
我正在通过Nginx你认为这是一个问题? – philipfwilson