作者:不语皓月 | 来源:互联网 | 2023-07-30 17:23
postman测试接口正常,如图:后端也已经配置允许跨域,我觉得问题应该是配置了但是没有进到这里吧,因为其他接口在前端是能够正常访问的,贴上配置代码:@Component
postman测试接口正常,如图:
后端也已经配置允许跨域,我觉得问题应该是配置了但是没有进到这里吧,因为其他接口在前端是能够正常访问的,贴上配置代码:
@Component
@Slf4j
public class AccessFilter implements HandlerInterceptor {
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
this.accessConfig(response);
return true;
}
/**
* 配置跨域
*
* @param response
*/
private void accessConfig(HttpServletResponse response) {
log.debug("我进来了吗第二层");
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Credentials", "true");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS, DELETE, PATCH");
response.setHeader("Access-Control-Expose-Headers", "*");
response.setHeader("Access-Control-Allow-Headers", "Authentication,Origin, X-Requested-With, Content-Type, Accept, x-access-token");
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Pragma", "no-cache");
response.setHeader("Expires ", "-1");
}
}
但是:前端却提示:
百度了很多方法,但是都没有解决,直到我看到了这篇博客,地址:https://blog.csdn.net/garyond/article/details/80192760
然后我在写的接口上面加了注解:@CrossOrigin,问题完美解决~,如图:
重启后端,前端访问,完美运行:
如果觉得有用大家可以点个赞丫~新人一枚,有什么问题也欢迎指正~这是我踩到的小小坑之一,庆幸能够解决啦~
参考博客:https://blog.csdn.net/garyond/article/details/80192760
每一个成功的程序员,都少不了百度。