【发布时间】:2019-11-25 23:05:37
【问题描述】:
我试图设置一个 ui 用于使用 angular8 上传 excel 文件。前端和后端 (nodejs) 应用程序在两个不同的端口中运行。单击上传按钮时出现错误。
已尝试添加此代码:
app.use(cors());
app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
res.setHeader('Access-Control-Allow-Credentials', true);
next();
});
还是一样的错误
打字稿:
url_='http://localhost:3000/product/upload-exel'
public uploader :FileUploader = new FileUploader({
url:this.url_
});
html:
<input type="file" id="file"
ng2FileSelect [uploader]="uploader"
[(ngModel)]="path">
选项http://localhost:3000/product/upload-exel 404(未找到) 从源“http://localhost:4000”访问“http://localhost:3000/product/upload-exel”处的 XMLHttpRequest 已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:“Access-Control-Allow-Origin”标头中的值当请求的凭证模式为“包含”时,响应不能是通配符“*”。 XMLHttpRequest 发起的请求的凭证模式由 withCredentials 属性控制。
【问题讨论】:
-
错误显示
The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'.。那么您可以尝试删除*并添加您的客户端主机名吗?由于您使用的是angular,请尝试使用localhost:4200而不是* -
你可以在前端使用代理吗? stackoverflow.com/questions/47536466/…
-
我已将服务器地址更改为“Access-Control-Allow-Origin”,但仍然无法再次出现同样的错误。
-
我尝试使用代理仍然无法正常工作