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());
}