首页 > 解决方案 > ng2-file-upload 不适用于 Angular 6

问题描述

我正在使用 Angular 6 中的 ng2-file-upload 包。我的后端是 PHP。

我可以选择文件,但是当我尝试单击上传时出现此错误:

Failed to load [api url]: Response to preflight request doesn't pass
access control check: 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'. Origin 'http://localhost:4200' is 
therefore not allowed access. The credentials mode of requests 
initiated by the XMLHttpRequest is controlled by the withCredentials 
attribute.

在我的 API 中添加了标头

header('Access-Control-Allow-Origin','http://localhost:4200/');
header('Access-Control-Allow-Methods', 'POST');
header('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
header('Access-Control-Allow-Credentials', true);

仍然有同样的问题。

谢谢。

标签: angular

解决方案


您可以添加这些而不是您正在使用的

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, PATCH');
header('Access-Control-Allow-Headers: Origin, Content-Type, X-Auth-Token , Authorization');
header('Access-Control-Max-Age: 1728000');

预检option请求被拒绝,因为您不允许该方法。


推荐阅读