Home >Backend Development >PHP Tutorial >Introduction to the method of submitting token in thinkphp ajax

Introduction to the method of submitting token in thinkphp ajax

不言
不言forward
2019-03-29 10:41:373728browse

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.

Introduction to the method of submitting token in thinkphp ajax

#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

Introduction to the method of submitting token in thinkphp ajax

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!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete