项目记录:跨域问题解决方案

前端首页查询所有功能代码:

    queryAll()  {
      fetch("http://localhost:8096/schedule/all",
          {
                method: 'GET',
                credentials: 'include'
              })
          .then((response)  =>  {
            if  (response.ok)  {
              return  response.json();
            }
            else  {
              return  Promise.reject(new  Error("Failed  to  fetch  data"));
            }
          })
          .then((data)  =>  {
            this.scheduleData  =  data;  //  将返回的数据赋值给scheduleData
            this.searchResult  =  data;
            console.log(data)
          })
          .catch((error)  =>  {
            console.error(error);
          });
    }

出现下列报错:

Access to fetch at 'http://localhost:8096/schedule/all' from origin 'http://localhost:5173' has been blocked by CORS policy: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'.

解决方案,在cors配置类加上以下代码,不要使用通配符:

 corsConfiguration.addAllowedOrigin("http://localhost:5173"); // 设置访问源地址
 corsConfiguration.addAllowedOrigin("http://localhost:5174"); // 设置访问源地址

因为登录界面也有跨域问题,所以5174也要加进去。

还有以下报错:

Access to fetch at 'http://localhost:8096/schedule/all' from origin 'http://localhost:5173' has been blocked by CORS policy: The value of the 'Access-Control-Allow-Credentials' header in the response is '' which must be 'true' when the request's credentials mode is 'include'.

解决方案,在拦截器功能代码第一行加上以下代码:

response.setHeader("Access-Control-Allow-Credentials", "true");

做完上述步骤后,跨域问题是解决了,用火狐浏览器也能得到token值了。其他功能也可以采用类似方法解决跨域问题。

相关推荐

  1. 项目记录问题解决方案

    2023-12-10 10:44:03       59 阅读
  2. 问题解决方案

    2023-12-10 10:44:03       32 阅读
  3. 问题解决方案

    2023-12-10 10:44:03       35 阅读
  4. 问题几种解决方法

    2023-12-10 10:44:03       27 阅读
  5. 解决问题

    2023-12-10 10:44:03       45 阅读
  6. 解决问题

    2023-12-10 10:44:03       28 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-10 10:44:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-10 10:44:03       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-10 10:44:03       82 阅读
  4. Python语言-面向对象

    2023-12-10 10:44:03       91 阅读

热门阅读

  1. js new 原理

    2023-12-10 10:44:03       55 阅读
  2. 排序算法——快速排序

    2023-12-10 10:44:03       58 阅读
  3. 在uniapp中,可以使用那些预定义的样式类

    2023-12-10 10:44:03       51 阅读
  4. 1-5、JDK API文档

    2023-12-10 10:44:03       55 阅读
  5. 从零开始搭建链上dex自动化价差套利程序(11)

    2023-12-10 10:44:03       53 阅读