Node.js 执行重定向No';时浏览器控制台出错;访问控制允许原点';请求的资源上存在标头
我有一个AngularJS应用程序,运行速度为。前端应用程序对服务器执行http请求。因此,我必须在响应头上设置CORS 我在客户端添加了:Node.js 执行重定向No';时浏览器控制台出错;访问控制允许原点';请求的资源上存在标头,node.js,express,cors,cross-domain,Node.js,Express,Cors,Cross Domain,我有一个AngularJS应用程序,运行速度为。前端应用程序对服务器执行http请求。因此,我必须在响应头上设置CORS 我在客户端添加了: delete $http.defaults.headers.common["X-Requested-With"]; $http.defaults.headers.common["Accept"] = "application/json"; $http.defaults.headers.common["Content-Type"] = "applicatio
delete $http.defaults.headers.common["X-Requested-With"];
$http.defaults.headers.common["Accept"] = "application/json";
$http.defaults.headers.common["Content-Type"] = "application/json";
在服务器端(ExpressJS):
当我调用//localhost:9000->//localhost:8088时,一切正常。但是,当我重定向到另一个url(//api.example.com/authorize)时,我在浏览器控制台中遇到以下错误:XMLHttpRequest无法加载//api.example.com/authorize?response_type=code。请求的资源上不存在“Access Control Allow Origin”标头。因此,不允许访问源“null”
你知道这是怎么回事吗?问题似乎与重定向和CORS有关。 状态代码为301、302、303、307或308的重定向将导致错误。 这一点正在改变,但现在您需要一种解决方法,例如在响应头或响应体中发回重定向URL,而不使用重定向 这在本文中得到了很好的解释 从那里引述: 在浏览器迎头赶上之前,唯一可行的选择似乎只有一个或多个 结合:
- 返回带有元刷新和/或Javascript位置更改的HTML
- 返回一个HTML,该HTML包含一个用重定向目标作为iframe源填充iframe的视口
- 显示用户必须单击才能访问内容的链接
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.header("Access-Control-Allow-Origin", "*");
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
next();
});
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.header("Access-Control-Allow-Origin", "*");
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
next();
});
app.get('/authorize', function (req, res) {
var response = 'https://api.example.com/authorize?response_type=code';
res.redirect(response);
});