java 解决前后端跨域问题

it2022-05-05  191

现状:在项目中,可能前后端需要分开写,这时将会出现拒绝访问情况 解决方案:在后端方法定义接收头文件即可(亲测有效) 在方法内部加入这三行代码,既可以解决前后端跨域问题

response.addHeader("Access-Control-Allow-Origin", "*"); response.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS"); response.addHeader("Access-Control-Allow-Headers", "Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers,token")

一下时项目中,具体实现方法

@ResponseBody @RequestMapping(value = "/findAll") public String FindAll(HttpServletResponse response) { System.out.println("进入 /image_face/findAll 方法"); response.addHeader("Access-Control-Allow-Origin", "*"); response.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS"); response.addHeader("Access-Control-Allow-Headers", "Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers,token") return ""; }

最新回复(0)