MadeInDreams February 2016

Ajax upload not working in Firefox and IE

The upload work as intended but not in firefox. There is no error msg and the post is set with a reply status of 200 OK. But the code stop in firefox and nothing get uploaded. I get no response from server beside status 200

EDIT

The POST event is not getting triggered by PHP when posted from Firefox;

The form;

<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>

After that in PHP I can catch the POST

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

Only in Chrome. This post is not set when using Firefox.

I have this code to upload files using 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;
                    });
                }
            }
        });
    });
});

The

Answers


Half Blood Prince March 2016

Make your ajax call synchronous by setting async : false. This must do the job.

Post Status

Asked in February 2016
Viewed 2,016 times
Voted 10
Answered 1 times

Search




Leave an answer