M
M
maestro072018-02-08 10:31:07
JavaScript
maestro07, 2018-02-08 10:31:07

Sending a form via ajax?

submit form via ajax. For the first time everything is fine. I want to resend again nothing works. what could be wrong? there is nothing in the console and network.

<form enctype="multipart/form-data" method='post' id="XLSX_form">
    {% csrf_token %} 
    <span class="btn btn-primary btn-block btn-file" disabled="disabled">
        Загрузить Excel<input id="id_file" name="file" type="file" disabled="disabled">
    </span>
</form>


function onChangeXLS(e) {
    e.preventDefault()
    var form = $(this);
    var data = new FormData();

    form.find(':input[name]').not('[type="file"]').each(function() {
        var field = $(this);
        data.append(field.attr('name'), field.val());
    });

    var filesField = form.find('input[type="file"]');
    var fileName = filesField.attr('name');
    var file = filesField.prop('files')[0];
    data.append(fileName, file);

    console.log("data", data);

    $.ajax({
        url: uploadXLSX,
        type: 'POST',
        data: data,
        contentType: false,
        cache: false,
        processData: false,
        success: function(data) {
            console.log('data', data);
            var arrayOfJSON = data;
            invoiceFileExt = 'json';
            for (var i = 0; i < arrayOfJSON.length; i++)
                invoiceList[i] = JSON.stringify(arrayOfJSON[i]);

            if(invoiceList.length > 0 && !_.isEmpty(invoiceList[0])){
                invoiceCount = 0;
                sendOneByOne();
                blockScreen("");
                controlBtns.show();
            } else {
                showAlert(Globals.textRu.empty_file);
            }
        }
    });
}
document.getElementById('XLSX_form').addEventListener('change', onChangeXLS);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Ainur Valiev, 2018-02-08
@maestro07

onchange worked its 1 time when choosing a picture. hang callback on click

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question