Last active
February 22, 2017 07:14
-
-
Save AshikNesin/7406d6550a03af0b19fd8e82e1fde00f to your computer and use it in GitHub Desktop.
[jQuery] Ajax form Submit
This file contains hidden or 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
$(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