Skip to content

Instantly share code, notes, and snippets.

@fastcodecoq
Last active August 29, 2015 14:02
Show Gist options
  • Select an option

  • Save fastcodecoq/9499e5a5d3efaaa82b37 to your computer and use it in GitHub Desktop.

Select an option

Save fastcodecoq/9499e5a5d3efaaa82b37 to your computer and use it in GitHub Desktop.

USO

Serializar formularios directamente a un objeto javascript, para luego enviarlos al server por Ajax.

Dependendcias: jQuery.

HTML

<form class="formularioLogueo">
      <input name="usuario" type="text">
      <input name="clave" type="password">
</form>

Javascript

En el controlador del evento submit incluir esto:

  var data = $("form.formularioLogueo").serializeObject();

  $.post("ruta_archivo_logueo", data)
   .done(function(rs){ alert("hecho"); })
   .fail(function(err){ alert("falló"); });
$.fn.serializeObject=function(){var c={};var b=this.serializeArray();$.each(b,function(){if(c[this.name]!==undefined){if(!c[this.name].push){c[this.name]=[c[this.name]]}c[this.name].push(this.value||"")}else{c[this.name]=this.value||""}});return c};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment