It seems like a simple issue but its been hour now breaking my head on this.
i have a form which accepts file from user submit to the server using ajax. Problem i am facing is in $(this).serialize()
which always returns an empty string. i have defined name attribute for input field. function too seems to be correct can anyone point out if i am missing something!!
There are many similar que's already on this but most answers say it needs name attribute which is already present.
Form:
<form action="/upload" name="upload_form" method="POST" id="upload-form">
<input type="file" name="file" value=""/><br />
<input type="submit" name="submit" value="Upload"/>
<input type="reset" name="clear" value="Clear"/>
</form>
Script
$(document).ready(function () {
$('#upload-form').on('submit', function(e) {
e.preventDefault();
console.log($(this).serialize());
$.ajax({
url : $(this).attr('action'),
type: "POST",
data: $(this).serialize(),
success: function (data) {
console.log(data);
},
error: function (jXHR, textStatus, errorThrown) {
alert(errorThrown);
}
});
});
});