【发布时间】:2013-11-26 09:54:49
【问题描述】:
我正在尝试从 angular.js 模块到 nodejs + express 服务器进行跨域调用,但我收到了未经授权的错误,或标头错误: 我遵循了 Cors 配置,但仍然遇到问题。
Angular.js 代码:
methods.getWelcomeApps = function (onSuccess, onError) {
function makeBasicAuth(user, password) {
var tok = user + ':' + password;
var hash = btoa(tok); // Base64 encoding
return "Basic " + hash;
}
var auth = makeBasicAuth(config.API.AppsList.username, config.API.AppsList.password);
$http.defaults.useXDomain = true;
$http({method: 'GET', url: config.API.AppsList.url, headers: {'Authorization': auth}})
.success(onSuccess)
.error(onError);
};
return methods;
});
And the node.js code:
allowCrossDomain = function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'X-Requested-With');
res.header('Access-Control-Allow-Headers', 'Content-Type');
if (req.method === 'OPTIONS') {
return res.send(200);
}
return next();
};
我错过了什么吗?
谢谢。
【问题讨论】:
-
而具体的错误是...?
-
您不需要也允许 OPTIONS 吗?
标签: angularjs http-headers cors