在后端服务器上设置适当的CORS响应标头
在你的后端服务器上,你需要设置适当的CORS响应标头来允许来自Angular应用程序的跨域请求。
示例代码:
在PHP中:
header("Access-Control-Allow-Origin: *"); header("Access-Control-Allow-Methods: POST, GET, OPTIONS"); header("Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With");
在Node.js中:
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(); });
当你开启你的服务器(即启动你的PHP或Node.js应用程序)时,你现在应该能够顺利上传文件并得到响应。