Home > Article > Web Front-end > Ajax uses Token to verify identity
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("用户名或密码错误"); } }
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); } } }) }
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!