可以在angular的http请求中使用{ withCredentials: true }选项来解决此问题。这将允许跨域请求,同时保留cookies和同源策略。
以下是代码示例:
从Angular端发起请求:
$http.get('http://example.com/api/data', { withCredentials: true }).then(function(response) { console.log(response); });
从Node端接受请求:
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', 'http://localhost:3000');
res.header('Access-Control-Allow-Credentials', true);
});
然后就可以在Angular应用中使用$http服务并发送含有withCredentials选项的请求来进行跨域请求了。
上一篇:Angular+NodejsExpress出现错误:跨源请求被阻止,同源策略不允许读取远程资源
下一篇:Angular+RxJs+WebpacknottreeshakingRxJsoperatorsandobservables”