Is there a way to achieve serialization without triggering the submit function for an ajax call? I've searched extensively for a solution to this issue without any luck. The java script function is invoked when a button within the form is clicked.
document.forms[FORMNAME].submit();
Instead :
var form = $(document.forms[FORMNAME]);
$.ajax({
type : "POST",
url : form.attr('action'),
data : form.serializeArray(),
success : function(data) {
console.log("2");
},
error : function() {
console.log("3");
},
complete: function() {
console.log("4");
}
});
An error is thrown when I run the code.
Uncaught TypeError: form.attr is not a function
I also attempted the following:
var form = $(document.forms[FORMNAME]);
$.ajax({
type : "POST",
url : $(form).attr('action'),
data : $(form).serialize(),
success : function(data) {
console.log("2");
},
error : function() {
console.log("3");
},
complete : function() {
console.log("4");
}
});
I can only access my form using $(document.forms[FORMNAME])
.
It seems to only function properly with this method, which is not ideal for my needs.
document.forms[FORMNAME].submit(function(event){
// Prevent form submission
event.preventDefault();
var $form = $(this);
// AJAX STUFF
});