function getFormData(form) { var indexed_array = {}; var inputs = $(form).find('[name]') inputs.each(function () { indexed_array[$(this).attr('name')] = $(this).val() if ($(this).prop('tagName').toLowerCase() == 'select' && $(this).val() == '') { indexed_array[$(this).attr('name')] = null } if ($(this).attr('type') == 'number' && $(this).val() == '') { indexed_array[$(this).attr('name')] = null } }) return indexed_array; } function register(form) { var url = form.attr('action'); var method = form.attr('method'); var body = getFormData(form); var success = $("#form-success"); var error = $("#form-error"); if (body.password != body.confirm_password) { error.text("Las contrasenias no coincide."); return; } grecaptcha.ready(function() { grecaptcha.execute('6LcmmRwqAAAAAEOXJ3wyBx52txrB3zw_UdTQ22rE', {action: 'submit'}).then(function(token) { body['g-recaptcha-response'] = token $.ajax({ type: method, url: url, data: JSON.stringify(body), dataType: "json", cache: false, success: function (data) { console.log(data); error.text(""); success.text("Usuario registrado correctamente"); setTimeout(function () { window.location.href = '/admin/login'; }, 500) }, error: function(xhr, status, e) { console.log(xhr.responseText) success.text(" ") var res = JSON.parse(xhr.responseText); if (res.message) { error.text(res.message); } else { error.text("Ha ocurrido un error al registrar el usuario"); } } }) }); }); } $(document).ready(function () { var form = $('#form-register'); form.on('submit', function (event) { event.preventDefault(); register(form); }); });