Post FormData not working in IE9
The following code does not send file data to the server in IE9.
FormData
() the object looks like a problem, but not sure how to resolve it.
I have used form.serialize()
but this is not a file upload.
I am hesitant to implement a JQuery file loader just for this function. Is there an easy way to load a file, similar to FormData ()?
// HTML
<form name='aform' action='upload.php'>
<input type='file' name='afile'>
<input type='text' name='qty' value='1'>
<input type='hidden' name='product_id' value='7'>
<a class='addToCartButton'>Add to cart</a>
</form>
// JS
$(document).on('click', '.addToCartButton', function(event)
{
var form_id = $(this).closest('form').attr('id');
var formElement = document.getElementById(form_id);
var odata = new FormData(formElement);
//var $form = $('#'+form_id);
$.ajax({
url: 'http://localhost/cart/add',
data: odata, //$form.serialize(),
type: 'POST',
processData: false, // tell jQuery not to process the data
contentType: false // tell jQuery not to set contentType
}).done(function(data)
{
var returnObject = jQuery.parseJSON(data);
switch(returnObject.status) {
case 'success':
alert('Item added to cart');
break;
case 'error':
alert('An error occured');
break;
case 'no_file':
alert('No file was detected');
break;
}
});
event.preventDefault();
});
+3
source to share
1 answer
This is because IE9 does not support it.
http://caniuse.com/#search=formdata
Fully related: Post formdata via XMLHttpRequest object in JS? (cross browser)
0
source to share