官网 : https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Access-Control-Allow-Headers
上述条件只要有一个不满足就会触发options请求;下面的example 是get请求,加了header就会触发
server1 浏览器页面是8082端口 ("http://localhost:8082")
<button onclick="CORS()"> 点击测试跨域</button>
function CORS() {
$.ajax({
url: "http://127.0.0.1:11111/computer-info/",
type: 'get',
headers: {
'myheader': 'headerValue'
},
success: function (result) {
console.log(result);
}
})
}
server2 11111端口 跨域配置(FilterRegistrationBean):
"http://localhost:8080";
"GET", "POST", "DELETE", "PUT", "OPTIONS";
"*"
"true"
....
CorsConfiguration config = new CorsConfiguration();
config.setAllowedOriginPatterns(corsProperties.getAllowedOriginPatterns());
config.setAllowedMethods(corsProperties.getAllowedMethods());
config.setAllowedHeaders(corsProperties.getAllowedHeaders());
config.setAllowCredentials(corsProperties.isAllowCredentials());
config.setMaxAge(corsProperties.getMaxAge());
....
server2 11111端口
"接口http://127.0.0.1:11111/computer-info/ 获取computer信息,略。。"
发送预检请求时请求头中带的参数;
预检请求响应头中带的参数;
发送真正请求时,请求头;
发送真正请求时响应头;