Ajax 无法在 Firefox 和 IE 中上传

Ajax upload not working in Firefox and IE

上传工作正常,但在 Firefox 中不正常。没有错误消息,并且 post 设置了回复状态 200 OK。但是代码在 firefox 中停止并且没有上传任何内容。我在状态 200

旁边没有收到服务器的响应

编辑

当从 Firefox post 编辑时,POST 事件没有被 PHP 触发;

表格;

<form id="avatar_form" method="post" action="ajax-php.php" enctype="multipart/form-data">
<input id="avatarFile" type="file" name="file" size="25" class="input" value="" data-type="file">
<input id="avatarUp" type="submit" value="Télécharger" disabled="disabled" name="avatarUp">
</form>

在那之后 PHP 我可以赶上 POST

if(isset($_POST["avatarUp"])){// deal with the file here}

仅在 Chrome。使用 Firefox 时未设置此 post。

我有这个代码可以使用 ajax 上传文件;

$(document.body).ready(function () {
    $(document.body).on('submit', '#avatar_form', function (e) {
        e.preventDefault(e);
        var $form = $(this);
        var formdata = (window.FormData) ? new FormData($form[0]) : null;
        var data1 = (formdata !== null) ? formdata : $form.serialize();
        alert(data1);

        $.ajax({
            url: $form.attr('action'),
            type: $form.attr('method'),
            contentType: false, 
            processData: false, 
            dataType: 'json', 
            data: data1,
            success: function (response) {
                console.log(response);
                if (response == 1) {
                    var item = load_content('profil_menu').then(function (response) {
                        console.log("Item x", response);
                        data = response;
                        $('#profil_menu').html(data);
                        return;
                    });
                }
            }
        });
    });
});

代码在 Chrome 中运行良好,但在 FirexFox 或 IE 中不行,我不太关心 IE,但它应该在 Firefox 中运行?

所以这个 Post 有什么问题?

通过设置 async : false 使您的 ajax 调用同步。这必须完成工作。