Access-Control-Allow-Credentials跨域问题

  1. 在前端向后端请求接口数据时在控制台报出错误:
    在这里插入图片描述
  2. 解决方法:前端:
    在config文件下的index.js中`
module.exports = {
  dev: {

    // Paths
    assetsSubDirectory: 'static',
    assetsPublicPath: '/',
    proxyTable: {
      '/api': {
        target: 'http://localhost:后端设置的端口号',
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/api': ''
        }
      }
    }

在后端也要配置跨域请求:

@Override
    public void addCorsMappings(CorsRegistry registry) {
        //所有请求都允许跨域
        registry.addMapping("/**")
                .allowCredentials(true)
                //添加起源
                .allowedOrigins("http://localhost:前端端口号")
                .allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
                .allowedHeaders("*")
                .maxAge(3600);
    }

注意的是不要忘记添加前端起源
3. 配置完之后问题就得到了解决

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐