ajax upload does not pass variables

Asked

Viewed 47 times

0

I’m not getting past 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
            });
        };
    });
});

How it appears in the browser console: quotesEditarUpload.php? [Object Formdata]&_=1441423098339

What am I doing wrong?

  • @Brunno did not work. Same problem.

  • we’ll have to wait for someone to mess with php to help you :PP just gave a sour guess hahaha' :DD

  • Thank you very much for the tip. If you have another I will be happy to test :)

  • Some light at the end of the tunnel?

  • @Brunno so worked the variables data: 'arquivo'+ $('.arquivo').prop('files')[0] +'&nomeArquivo='+ $('.arquivo').val() +'&id_cotacao='+ $('#ID_Cotacao').val(), but the file is not sent, only the variables. Knows how to do?

1 answer

2

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

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

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

With the exception of the request method, I don’t see another problem with your code interfering with sending the FormData.

Excerpt from the code:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.