I am trying to upload files using jQuery and AJAX. I have used FormData
object which is supported in HTML5. I am using IE11. Below is my code:
<form id="tradeForm" method="post" action="/trade.action?method=addTrade" enctype="multipart/form-data">
Trade Type : <input type="text" id="tradeType" name="tradeType">
Trade Document : <input type="file" id="attachedFile" name="attachment" size="40">
</form>
I've tried using $.post
:
$("#tradeForm").submit(function(event){
event.preventDefault();
var form = $(this);
var formData = new FormData(form);
url = form.attr("action");
$.post(url, formData, function(data) {
console.log(data);
});
});
And also using $.ajax
$("#tradeForm").submit(function(event){
event.preventDefault();
var form = $(this);
var formData = new FormData(form);
url = form.attr("action");
$.ajax({
url: url,
type: "POST",
data: formData,
processData: false, // tell jQuery not to process the data
contentType: false // tell jQuery not to set contentType
}).done(function(data) {
console.log(data);
});
});
I get the following errors:
Argument not optional
When $.post
method is used
SCRIPT7002: XMLHttpRequest: Network Error 0x2ef3, Could not complete the operation due to error 00002ef3
When $.ajax
method is used. How can I solve these errors?