Request ajax with append before opening modal

Asked

Viewed 476 times

0

For some reason I’m not getting this append to work.

function mostrarUploads(id) {
    $.ajax({
      url: "/painel/mostrarUploads",
      async: false,
      method: 'POST',
      data: {_token: jQuery(".token").val(), id: id},
      success: function(e) {
        e.forEach(function(item, indice) {
          $('#bodyTabelaUploads tbody').append('<tr><td>TESTE</td></tr>');
        });
      }
    });
}   

I have this table:

<table class="table table-bordered table-striped table-hover" id="tabelaUploads">
                        <thead>
                            <tr>
                                <th>Nome do arquivo</th>
                                <th>Ações</th>
                            </tr>
                        </thead>

                        <tbody id = "bodyTabelaUploads">

                        </tbody>
                    </table>

I’m calling the method before opening a modal. It’s an asynchronous method, but I’m using async: false.

Is it because this method is asynchronous, the modal opens before, and I don’t see the line being added? OBS: console.log(e) returns results!!

  • Only to complement: Synchronous Ajax is deprecated since the jQuery 1.8, so I recommend that you try to do everything asynchronously

  • Okay, thank you!!!

2 answers

1


first, async: false is always a bad idea. The $.ajax returns a promise, then, open your modal in the continuation of the same.

function mostrarUploads(id) {
  return $.ajax({
    url: "/painel/mostrarUploads",
    method: 'POST',
    data: {_token: jQuery(".token").val(), id: id},
    success: function(e) {
      e.forEach(function(item, indice) {
        $('#bodyTabelaUploads tbody').append('<tr><td>TESTE</td></tr>');
      });
    }
  });
}

mostrarUploads(id).done(function(data, textStatus, jqXHR) {
  //exibir modal.
}).fail(function(jqXHR, textStatus, errorThrown) {
  //exibir mensagem de erro.
});
  • Thank you for your comment. When I use it like this, the browser returns "cannot read Property done of Undefined" as if the function did not exist. It is stated upon use of the method. Any idea what it might be? I will try to use the $.when() I read about as well.

  • Look, your answer gave me a good inspiration that worked. I used . done in the ajax request itself. The only problem I see in this approach is that it can take a long time to load this and the user experience goes bad. However, it is still a solution. Someone knows some better way to do this?

  • @but if you make a async: false will not make the return faster, the difference is that the page will stop responding while waiting for the return.

1

The problem with your code is that your table is called tabelaUploads, and you’re looking for a tbody in the body of this table here: $('#bodyTabelaUploads tbody'), which will return nothing.

Change $('#bodyTabelaUploads tbody') for $('#tabelaUploads tbody')

HOWEVER

I would like to point out that synchronous AJAX requests are already deprecated and should be avoided, as eventually they will probably be totally removed (many, many years from now).

JQuery himself warns us of this:

inserir a descrição da imagem aqui

  • I hadn’t really seen this detail of tbody, thank you! But I still need help on the timing issue. I think I need to finish this requisition before I open the modal. Or it will be possible to open the modal and even so after it opened I modify the table 'dynamically' with the append?

  • You can do it in many ways. You can open the modal and show something like carregando... while making the request. Or you can show the modal only when the request is finished

  • Yes, thank you for the answer! I believe that there is something that runs away from this.

  • You have no way to escape any server delay. You just have to warn the user that you are waiting, and locking the screen with a synchronous request is not the best way :)

Browser other questions tagged

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