项目场景:
Java 文件下载报错
问题描述
Access to XMLHttpRequest at ‘http://localhost:8080/api/xx/xx’ from origin ‘http://localhost:8099’ has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.
解决方案:
添加一下代码即可:
response.addHeader("Access-Control-Allow-Origin", "*");
response.addHeader("Access-Control-Expose-Headers","*");
response.addHeader("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE");
response.addHeader("Access-Control-Allow-Headers", "Content-Type");
response.addHeader("Access-Control-Allow-Credentials","true");