后端接值验证
$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;
});
},
})