在我验证用户将输入数据输入到必填字段但是根本没有执行ajax请求之后,我尝试使用ajax来验证用户凭据。我不知道是什么导致了所有因素,但我真的很感激任何线索和答案。这是我的代码
/*global $*/
/*global _*/
$(document).ready(function() {
'use strict';
$('#login-form').on('submit', function(event) {
var form_items = {
username: { required: true, name: 'Username' },
password: { required: true, name: 'Password' },
token: {}
};
var $login_form = $(this);
var submit_form = false;
var validation = new Validation( form_items );
validation = validation.check( $login_form['context'] );
// if the error list display is there, remove it
$('.bg-danger').parent().remove();
// If any element has the class for errors, remove the class
$('.has-error').removeClass('has-error');
if ( validation.passed() ) {
var data = validation.getData();
$.ajax({
method: "GET",
url: "ajax/login.ajax.php",
data: data
}).done(function ( response ) {
console.log( response );
});
} else {
var errors = validation.getErrors();
var display = '<div class="form-group"><ul class="bg-danger">';
_.each( errors, function(elem, index, list) {
var $form_element = $('#'+index); // get form element
$form_element.parent().parent().addClass('has-error');
display += '<li>' + elem + '</li>';
});
display += '</ul></div>';
$login_form.prepend( display );
}
if ( !submit_form ) {
event.preventDefault();
}
});
});