axios跨域问题, 这种情况下 withCredentials 不能为true

前端报错信息如下:

Access to XMLHttpRequest at 'http://127.0.0.1:8000/XXX' from origin 'http://127.0.0.1:8080' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: 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'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

写了一个最简单的前端测试页面跨域访问后端,访问正常,确定后端是允许跨域的。

认真研读后,才明白:

当服务器端的Access-Control-Allow-Origin值设置为任意网站[*] 时,axios的 withCredentials 不能 = true 。

若需要跨域携带cookies,则以下3点都要做:1、服务器端的Access-Control-Allow-Origin值设置为指定网站(白名单),2、服务器端要设置Access-Control-Allow-Credentials   3、浏览器端的 withCredentials 必须 = true

当服务器端的Access-Control-Allow-Origin 没有设置时,无论浏览器端的 withCredentials 为true 还是 false 都不能跨域

之前查看资料,一直错误理解为:服务器端要设置Access-Control-Allow-Origin 且 浏览器端要 withCredentials=true 才能跨域。-->这个理解是错的!

相关推荐

  1. axios问题, 这种情况 withCredentials true

    2023-12-15 11:58:02       61 阅读
  2. 解决uniApp 中直接使用 Axios问题

    2023-12-15 11:58:02       35 阅读
  3. 问题+解决express

    2023-12-15 11:58:02       37 阅读
  4. 问题浅析

    2023-12-15 11:58:02       60 阅读

最近更新

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

    2023-12-15 11:58:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-15 11:58:02       101 阅读
  3. 在Django里面运行非项目文件

    2023-12-15 11:58:02       82 阅读
  4. Python语言-面向对象

    2023-12-15 11:58:02       91 阅读

热门阅读

  1. 746.使用最小花费爬楼梯

    2023-12-15 11:58:02       60 阅读
  2. C# DataTable 总结常用方法

    2023-12-15 11:58:02       52 阅读
  3. C# 德语法语解析浮点数不正确的问题记录

    2023-12-15 11:58:02       67 阅读
  4. 解析Python的Lambda函数:【理解】与【运用】

    2023-12-15 11:58:02       52 阅读
  5. 关于C#反射概念,附带案例!

    2023-12-15 11:58:02       62 阅读
  6. Go并发编程:保障安全与解锁奥秘

    2023-12-15 11:58:02       61 阅读
  7. 异常处理返回结构体,做到全局统一

    2023-12-15 11:58:02       67 阅读
  8. electron这样使用更安全

    2023-12-15 11:58:02       95 阅读