The following code does not post the file data to the server in IE9.
FormData
() object looks to be the issue but not sure how to resolve it.
I used form.serialize()
but this is not uploading the file.
Im hesitant to implement a JQuery file uploader just for this function. Is there a simple way to upload 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();
});