Skip to content

Instantly share code, notes, and snippets.

@AshikNesin
Last active February 22, 2017 07:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AshikNesin/7406d6550a03af0b19fd8e82e1fde00f to your computer and use it in GitHub Desktop.
Save AshikNesin/7406d6550a03af0b19fd8e82e1fde00f to your computer and use it in GitHub Desktop.
[jQuery] Ajax form Submit
$(document).ready(function() {
$.validate();
// Object as Array
$.fn.serializeObject = function() {
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name] !== undefined) {
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;
};
$("#contactForm").submit(function(event) {
event.preventDefault();
var formData = {};
formData = $('#contactForm').serializeObject();
$.ajax({
url: 'http://example.com/endpoint',
type: "POST",
data: JSON.stringify(formData),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(res) {
console.log(res);
}
})
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment