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