Home >Backend Development >PHP Tutorial >Introduction to the method of submitting token in thinkphp ajax
This article brings you an introduction to the method of submitting token in thinkphp ajax. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
When you forget your password, you need to use ajax to submit it. I am afraid that the text message will be stolen, so I use the token that comes with thinkphp to do a simple verification (combined with the verification code).
See that the token in the form is actually verified together with the form data, which is actually equivalent to a field in the form.
#Then I thought, just submit the token together with other fields in ajax.
function setCodeAjax(){ var mobile = $("[name='phone']").val(); var token = $("[name='__token__']").val(); $.ajax({ data:{'mobile':mobile,'__token__':token}, dataType:'json', type:'post', url:"XXX", success:function (d) { if(d.code == 0 ){ //成功处理 }else{ //失败处理 } } }) }
The format of submission is exactly the same as form submission
The background can be verified according to the verification in the tp manual.
For example:
$validate = Validate::make([ 'mobile' => 'require|token' ]); $data = $this->request->post(); $result = $validate->check($data); if ($result != true) { return _codeMsg('1001',$result); } //后续处理
However, if you use ajax for verification, please note that if the token has been submitted for verification, then the token will become invalid and needs to be changed manually on the front end.
This article has ended here. For more other exciting content, you can pay attention to the PHP Video Tutorial column on the PHP Chinese website!
The above is the detailed content of Introduction to the method of submitting token in thinkphp ajax. For more information, please follow other related articles on the PHP Chinese website!