解决前端跨域的问题.Access to XMLHttpRequest at http://xxx.xxx from origin 'http://localhost:8000' has been bl

1、前端浏览器报错如下:

Access to XMLHttpRequest at http://xxx.xxx from origin 'http://localhost:8000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: Redirect is not allowed for a preflight request.

解决前端报错
项目是springboot框架,前后端分离,需要跨域,当前前端可以用JSONP解决,但是java端如何解决呢?

因为是springboot框架,所以好多都可以用注解解决问题,所以就用到了@CrossOrigin,这个是解决跨域问题相当好的一种方法,当然还有写个全局配置类,因为我这里只是个别方法需要跨域,所以不用全局配置。
@CrossOrigin只在方法中配置跨域时,只需要在方法名上加上这一注解就行吗,如果是通用所有的ip访问,就再设置(origins = “*”,maxAge = 3600),当然也可以细化具体的ip。

@CrossOrigin(origins = “*”,maxAge = 3600)
@GetMapping("/getinfos")
public ResponseEntity getInfos() {
return ResponseEntity.ok(userService.getInfos());
}

posted @ 2020-11-09 10:42  乖怪丶  阅读(12410)  评论(0编辑  收藏  举报
……