ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

laravel8 ajax提交表单,后端validate验证的错误信息提示

2021-11-28 21:06:24  阅读:64  来源: 互联网

标签:username errors laravel8 res required captcha ajax validate password


后端接值验证

 $data = $request->validate([
            'username' => 'required',
            'password' => 'required',
            'captcha' => 'required|captcha'
        ],[
            'username.required' => '用户名不能为空',
            'password.required' => '密码不能为空',
            'captcha.required' => '验证码不能为空',
            'captcha.captcha' => '验证码不正确',
        ]);

前端ajax使用错误回调

   $.ajax({
            url:"{{route('test.login')}}",
            type:'POST',
            data:{
                username:username,
                password:password,
                captcha:captcha,
                _token:"{{csrf_token()}}"
            },
            success:function (res){
                if(res.code==200){
                    alert(res.msg);
                    location.href='{{route('test.index.index')}}';
                }else {
                    alert(res.msg);
                    return false;
                }
            },
            error: function(res) {
                errors = res.responseJSON.errors;
                var form_errors = '';
                $.each(errors, function(i) {
                   alert(errors[i])
                    return false;
                });

            },
        })

标签:username,errors,laravel8,res,required,captcha,ajax,validate,password
来源: https://blog.csdn.net/Lorientasn/article/details/121597572

专注分享技术,共同学习,共同进步。侵权联系[admin#icode9.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有