Home > Web Front-end > JS Tutorial > body text

Detailed example of jQuery Ajax using Token to verify identity

小云云
Release: 2017-12-27 11:08:24
Original
2700 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 pass the request header and Token to the backend. This article mainly introduces the example code of jQuery Ajax using Token to verify identity. Friends in need 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

Finally, I wish you all fewer bugs

Related recommendations:

Example showing the use and verification of php token

Complete login verification by using node.js+captchapng+jsonwebtoken

Token verification for WeChat public platform development And message processing methods

The above is the detailed content of Detailed example of jQuery Ajax using 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!