其他分享
首页 > 其他分享> > laravel8 ajax提交表单,后端validate验证的错误信息提示

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

作者:互联网

后端接值验证

 $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