CORS跨域原理詳解
Spring解決跨域
- 響應(yīng)頭設(shè)置跨域
@RequestMapping(value = "/ajax")
public @ResponseBody
Customer ajax(Integer id, HttpServletResponse response) {
Customer customer = customerService.queryCustomerById(id);
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "x-requested-with,Authorization");
response.setHeader("Access-Control-Allow-Credentials", "true");
return customer;
}
- Spring注解跨域
@CrossOrigin 可添加到方法上竟趾,也可添加到類上
// orgins=''http://localhost:63343"表示允許域名為
http://localhost:63343 訪問該方法
// origins="*", 表示允許所有其他的網(wǎng)站訪問該方法
@CrossOrigin(origins = "http://localhost:63343")
@RequestMapping(value = "/ajax")
public @ResponseBody
Customer ajax(Integer id, HttpServletResponse response) {
Customer customer = customerService.queryCustomerById(id);
return customer;
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者