{ "/api": { "target": "http://example.com", "secure": false, "changeOrigin": true } }
修改target值为实际请求的网站地址,然后在命令行中运行以下命令启动代理服务器:
ng serve --proxy-config proxy-config.json
app.use(function(req, res, next) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); next(); });
html2canvas(document.getElementById('my-element'), { useCORS: true }).then(function(canvas) { // ... });
以上是解决Angular JSPDF html2canvas图片被CORS策略阻止的方法,根据具体情况选择合适的方案即可。