Created
October 14, 2013 12:43
-
-
Save ccarrasc/6975003 to your computer and use it in GitHub Desktop.
Serialize form input fields to JSON and POST with jQuery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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