当前位置:主页 > 查看内容

关于Axios中写了axios.defaults.withCredentials = true,携带Se

发布时间:2021-06-29 00:00| 位朋友查看

简介:文章目录 前言 一、修改之前 1.登录 2.登录之后读取 二、解决方案 三、修改之后 1.登录 2.登录之后读取 前言 这次项目登录的时候利用axios传输的时候发现后台接收的到sessionID但是返回前台的时候cookie却没有sessionID 网上大多数都是axios.defaults.withCr……


前言


这次项目登录的时候利用axios传输的时候发现后台接收的到sessionID但是返回前台的时候cookie却没有sessionID,
网上大多数都是axios.defaults.withCredentials = true,但是没用,解决了一下午,终于找到了答案

一、修改之前

1.登录

代码如下(示例):

 axios({
        widthCredentials: true,
          method: 'post',
          url: "http://location:8080/api/guojia/all/login",
          data: _this.form
      }).then(res => {
          console.log(res)
          if (res.data != null && res.data != "") {
              localStorage.setItem("position", res.data.position.pname)
              sessionStorage.setItem("tabs", "");
              location.href = "../../index.html"
          } else {
              this.$message.error('账号或者密码错误');
          }
      })

2.登录之后读取

代码如下(示例):

 axios({
        method: 'get',
        url: "http://127.0.0.1:8080/api/guojia/all/getUser",
        widthCredentials: true
      }).then(res => {
        _this.User = res.data
        $(".allpage").hide();
        showsItem = _this.User.position.operatingList;
        pageInit()
        $("#hhhh").load("index2.html")
      })

照网上大多数的方法,我已经加上了axios.defaults.withCredentials = true,但是还是没有解决


二、解决方案

不知道大家注意到没有我登录的URL和读取的URL有什么不同,在登录的时候我写的是http://location:8080,看群友说,有时候location返回时前端无法保存到cookie,这是错误一,错误二是读取的时候我写的是http://127.0.0.1:8080,这就导致了两次访问的地址不一样,当然取不到SessionID

三、修改之后

1.登录

代码如下(示例):

 axios({
        widthCredentials: true,
          method: 'post',
          url: "http://127.0.0.1:8080/api/guojia/all/login",
          data: _this.form
      }).then(res => {
          console.log(res)
          if (res.data != null && res.data != "") {
              localStorage.setItem("position", res.data.position.pname)
              sessionStorage.setItem("tabs", "");
              location.href = "../../index.html"
          } else {
              this.$message.error('账号或者密码错误');
          }
      })

2.登录之后读取

代码如下(示例):

 axios({
        method: 'get',
        url: "http://127.0.0.1:8080/api/guojia/all/getUser",
        widthCredentials: true
      }).then(res => {
        _this.User = res.data
        $(".allpage").hide();
        showsItem = _this.User.position.operatingList;
        pageInit()
        $("#hhhh").load("index2.html")
      })

;原文链接:https://blog.csdn.net/BiYvGe/article/details/115664343
本站部分内容转载于网络,版权归原作者所有,转载之目的在于传播更多优秀技术内容,如有侵权请联系QQ/微信:153890879删除,谢谢!

推荐图文


随机推荐