ajax upload does not pass variables

0

I can not pass the variables in the ajax method.

<input type="file" id="id-input-file-2" name="arquivo" class="form-control arquivo" />
<input type="hidden" name="ID_Cotacao" id="ID_Cotacao" value="555">

.

$(function(){
    $('.arquivo').change(function(e){
        if ($('.arquivo').val() != "") {
            e.preventDefault();
            var formData = new FormData();

            formData.append('arquivo', $('.arquivo').prop('files')[0]);
            formData.append('nomeArquivo', $('.arquivo').val());
            formData.append('id_cotacao', $('#ID_Cotacao').val());

            $.ajax({
                url: 'cotacoesEditarUpload.php',
                data: formData,
                type: 'GET',
                success: function(){
                    alert("Enviado com sucesso."),
                    $(".listaArquivo").load(window.location + " .listaArquivo")
                },
                processData: false,
                cache: false,
                contentType: false
            });
        };
    });
});

As it appears in the browser console: cotacoesEditUpload.php? [object FormData] & _ = 1441423098339

What am I doing wrong?

    
asked by anonymous 05.09.2015 / 05:24

1 answer

2

I'm assuming you want to do a file upload.

File upload can not be performed on a GET request. Not that it's impossible (it would be necessary to read the data from the file and encode it into something like base64 or some other representation that could be placed in an HTTP query).

Use the POST or PUT method to upload the file. On the server side, each of them has particularities in data processing, with POST more traditional especially using PHP.

Except for the request method, I do not see another problem in your code interfering with sending FormData .

Code snippet:

$.ajax({
    url: 'cotacoesEditarUpload.php',
    data: formData,
    type: 'POST',
    success: function(){
    
05.09.2015 / 16:01