When using the jquery validation plug-in validationplugin for remote verification, the document says that the server needs to return a json string. true indicates that the parameter is valid. & quot; false & quot;, undefined, and null indicates that the parameter is invalid. Verify whether the user name has been registered. If the user name has been registered, return... when using the jquery validation plug-in validation plugin for remote verification, the document says that the server needs to return a json string. true indicates valid, "false", undefined, and null indicates invalid.
Verify whether the user name has been registered. If the user name has been registered, false is returned. If the user name has not been registered, true is returned. The php server (laravel framework) is written as follows:
public function verifyName(Request $request) { $username = $request->input('name', ''); $user = User::where('name', $username)->first(); if ($username == $user->name) { return json_encode(false); }else{ return json_encode(true); } }
The front-end js Code is as follows:
《script》 $(function () { $.ajaxSetup({ headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') } }); $("#register").validate({ rules: { name: { required: true, rangelength: [4, 30], remote: { url: "{{ url('services/validation/verify_name') }}", type: "post" } } }, errorClass: "has-danger", validClass: "has-success", //success: "valid", highlight: function (element, errorClass) { $(element).fadeOut(function () { $(element).fadeIn(); }); $(element).closest(".form-group").addClass(errorClass); }, unhighlight: function (element, errorClass) { $(element).closest(".form-group").removeClass(errorClass); }, errorPlacement: function (error, element) { error.insertAfter(element); } }); });《script》
Problem:
If 'false' is returned, it can be returned normally. If you want to return 'true', it cannot be returned. Trying to get property of non-object appears. I do not know why.
Reply content:
When using the jquery validation plug-in validation plugin for remote verification, the document says that the server needs to return a json string. true indicates valid, "false", undefined, and null indicates invalid.
Verify whether the user name has been registered. If the user name has been registered, false is returned. If the user name has not been registered, true is returned. The php server (laravel framework) is written as follows:
public function verifyName(Request $request) { $username = $request->input('name', ''); $user = User::where('name', $username)->first(); if ($username == $user->name) { return json_encode(false); }else{ return json_encode(true); } }
The front-end js Code is as follows:
《script》 $(function () { $.ajaxSetup({ headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') } }); $("#register").validate({ rules: { name: { required: true, rangelength: [4, 30], remote: { url: "{{ url('services/validation/verify_name') }}", type: "post" } } }, errorClass: "has-danger", validClass: "has-success", //success: "valid", highlight: function (element, errorClass) { $(element).fadeOut(function () { $(element).fadeIn(); }); $(element).closest(".form-group").addClass(errorClass); }, unhighlight: function (element, errorClass) { $(element).closest(".form-group").removeClass(errorClass); }, errorPlacement: function (error, element) { error.insertAfter(element); } }); });《script》
Problem:
If 'false' is returned, it can be returned normally. If you want to return 'true', it cannot be returned. Trying to get property of non-object appears. I do not know why.
《script》 $(function () { $.ajaxSetup({ headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') } }); $("#register").validate({ rules: { name: { required: true, rangelength: [4, 30], remote: { url: "{{ url('services/validation/verify_name') }}", type: "post", data:{ name:function(){ return $("input[name='name']").val(); } } } } }, errorClass: "has-danger", validClass: "has-success", //success: "valid", highlight: function (element, errorClass) { $(element).fadeOut(function () { $(element).fadeIn(); }); $(element).closest(".form-group").addClass(errorClass); }, unhighlight: function (element, errorClass) { $(element).closest(".form-group").removeClass(errorClass); }, errorPlacement: function (error, element) { error.insertAfter(element); } }); });《script》
Did your front-end code forget to post data to the background?
Data :{
name:function(){ return $("input[name='name']").val();}
}