"No 'Access-Control-Allow-Origin' header"跨域問題CROS
問題描述

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:8020' is therefore not allowed access.
瀏覽器跨域測試
- 直接使用瀏覽器開發者工具, 在控制臺執行代碼測試
// 正常情況
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://localhost:8080/test.pdf');
xhr.send(null);
xhr.onload = function(e) {
var xhr = e.target;
console.error(xhr.responseText);
}
// 跨域
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://127.0.0.1:8080/themes/zone/casepdf/177.pdf');
xhr.send(null);
xhr.onload = function(e) {
var xhr = e.target;
console.log(xhr.responseText);
}
Tomcat 跨域配置
web.xml
<filter>
<filter-name>CorsFilter</filter-name>
<filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
<init-param>
<param-name>cors.allowed.origins</param-name>
<param-value>*</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CorsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Spring跨域配置
http://www.rzrgm.cn/52liming/p/9535636.html
參考博客
Spring MVC通過CROS協議解決跨域問題
spring 官方文檔:
鏈接:https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-cors

浙公網安備 33010602011771號