Introduction to the method of submitting token in thinkphp ajax

不言
Release: 2023-04-05 18:20:02
forward
3685 people have browsed it

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{
                            //失败处理
                        }
                    }
                })
        }
Copy after login

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);
        }
        
        //后续处理
Copy after login

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!

Related labels:
source:segmentfault.com
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!