Home > Web Front-end > JS Tutorial > Ajax uses Token to verify identity

Ajax uses Token to verify identity

小云云
Release: 2018-02-03 14:15:13
Original
1820 people have browsed it

Because I have done several backends recently, Token verification identity operations are often involved in the backend, so here is a record of how to transfer the request header and Token to the backend. This article mainly introduces the example code of jQury Ajax using Token to verify identity. Friends who need it can refer to it. I hope it can help everyone.


success:function(dat){
          console.log(dat);
            if(dat.code==1){
              sessionStorage.setItem('token',dat.data.access_token);
              //这里设置缓存存储Token
              sessionStorage.setItem('user',userName);
              location.href = "index.html";
            }else{
              $(".tip2").html("用户名或密码错误");
            }
        }
Copy after login


function edit(obj,obj1,url){//要关闭的弹框类名 form表单id 编辑接口
  var token1 ='basic '+ window.sessionStorage.token;
  //这里的格式是按后台要求的 通过缓存读取Token
  var url = browerUrl+url;
  var obj1 = "#"+obj1;
  var formData = new FormData($(obj1)[0]);
  for(var x of formData){
    console.log(x)
  }
  $.ajax({
    type:"post",
    async:true,
    cache: false,
    contentType: false,
    processData: false,
    url:url,
    data:formData,
    //在请求前设置请求头 在请求头里面设置设置请求头的信息
    beforeSend: function(request) {
            request.setRequestHeader("Authorization", token1);
          },
    //或者直接设置请求头
    //headers:{"Authorization", token},
    success:function(dat){
      console.log(dat);
      if(dat.code==1){
        messageShow("操作成功");
        tanClose(obj);
        setTimeout(reload(),3000);
      }
      else if(dat.code==309){
        backHome();
      }
      else{
        console.log(dat.msg);
      }
    }
  })
}
Copy after login

Related recommendations:

Token verification and messaging for WeChat public platform development Processing method

The above is the detailed content of Ajax uses Token to verify identity. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template