Skip to content

Instantly share code, notes, and snippets.

@ccarrasc
Created October 14, 2013 12:43
Show Gist options
  • Save ccarrasc/6975003 to your computer and use it in GitHub Desktop.
Save ccarrasc/6975003 to your computer and use it in GitHub Desktop.
Serialize form input fields to JSON and POST with jQuery
// Using <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
var array = $('form').serializeArray();
var json = {};
$.each(array, function() {
json[this.name] = this.value || '';
});
$.ajax({
type: "POST",
url: '/form-post-url',
data: json,
success: function() { /* Do something */ },
error: function() { /* Do something */ },
dataType: 'json'
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment