文件上传已被 CORS 策略阻止
Posted
技术标签:
【中文标题】文件上传已被 CORS 策略阻止【英文标题】:file uploading has been blocked by CORS policy 【发布时间】:2020-05-08 05:51:19 【问题描述】:我正在尝试通过角度 ng2-file upload 上传文件。
我有 2 个本地服务器:
http://localhost:4200 用于 angular 和 http://localhost 提供 php 文件
当我将文件发送到 php 服务器进行上传时出现错误:
从源访问“http://127.0.0.1/index.php”处的 XMLHttpRequest “http://localhost:4200”已被 CORS 策略阻止:响应 预检请求未通过访问控制检查:否 请求中存在“Access-Control-Allow-Origin”标头 资源。
【问题讨论】:
这可能会有所帮助:***.com/questions/42296349/… 【参考方案1】:在您的 php 中添加以下标头:
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, DELETE, PUT');
header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization');
header('Access-Control-Allow-Credentials: true');
【讨论】:
以上是关于文件上传已被 CORS 策略阻止的主要内容,如果未能解决你的问题,请参考以下文章
.Net Core 仅在上传文件时被 CORS 策略错误阻止
仅在文件上传中出现 Asp.Net Core API CORS 策略错误
从源“null”访问“http://github/..file.json”处的 XMLHttpRequest 已被 CORS 策略阻止: