Ajax Jquery File Uploader无法在IE11& Microsoft Edge

时间:2015-10-21 09:20:48

标签: javascript jquery ajax file-upload internet-explorer-11

我有ajax jquery文件上传器,它在Chrome,Firefox中工作正常,但不适用于IE和Microsoft Edge。它没有给我任何回应,我尝试了所有可能的解决方案,但仍然没有工作..

这是我的JS:

$('form').on('submit', function(event) {
    event.stopPropagation();
    event.preventDefault();


    $.ajax({
        url: 'upload.php',
        type: 'POST',
        data:  new FormData(this),
        cache: false,
        dataType: 'json',
        processData: false,
        contentType: false,
        success: function(data, textStatus, jqXHR)
        {
            console.log(data)


        },
        error: function(jqXHR, textStatus, errorThrown)
        {
            console.log('ERRORS: ' + textStatus);
        }
    });

});

和我的表格:

<form method="post" enctype="multipart/form-data">
  <input name="file[]" type="file" required multiple />
  <input type="radio" name="something" value="1"> 1
  <input type="radio" name="something" value="2"> 2
</form>

如何修复它??

1 个答案:

答案 0 :(得分:0)

我觉得问题在于您将data分配给ajax。当您在this中说new FormData时,它实际上是ajax call而不是form。所以最好把这个概念放在外面,然后再试一次。

$('form').on('submit', function(event) {
    //event.stopPropagation();//I don't think this is necessary
    event.preventDefault();
    var formdata=new FormData($('form').get(0)) //try with this here
    $.ajax({
        url: 'upload.php',
        type: 'POST',
        data:  formdata,
        cache: false,
        dataType: 'json',
        processData: false,
        contentType: false,
        success: function(data, textStatus, jqXHR)
        {
            console.log(data)
        },
        error: function(jqXHR, textStatus, errorThrown)
        {
            console.log('ERRORS: ' + textStatus);
        }
    });
});