Skip to content

Instantly share code, notes, and snippets.

@z2015
Created August 3, 2016 09:27
Show Gist options
  • Save z2015/82b5fa85849fd888286d160c6b7f44cf to your computer and use it in GitHub Desktop.
Save z2015/82b5fa85849fd888286d160c6b7f44cf to your computer and use it in GitHub Desktop.
jQuery post json data
$.fn.serializeObject = function() {
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name]) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};
var myJSObject = $('#form').serializeObject();
$.ajax(url, {
data : JSON.stringify(myJSObject),
contentType : 'application/json',
type : 'POST',
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment