CORS跨域原理详解
Spring解决跨域
- 响应头设置跨域
@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="*", 表示允许所有其他的网站访问该方法
@CrossOrigin(origins = "http://localhost:63343")
@RequestMapping(value = "/ajax")
public @ResponseBody
Customer ajax(Integer id, HttpServletResponse response) {
Customer customer = customerService.queryCustomerById(id);
return customer;
}
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。