Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

485
Visualizações
How to send an AJAX PUT request on page unload without it cancelling?

I'm trying to send a PUT request on the unload event, however the request is always cancelled.

Answers here suggests it's

  1. not possible: How to perform an ajax call on page unload?

  2. possible: How do I send an AJAX request upon page unload / leaving?

Can I send an AJAX request on unload, if so how can I without being cancelled.

Note: I don't care about the response, I don't need a response. Just need to send some info to the server saying the page was closed without keep alive checks.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

All you need to do is make the ajax call synchronous. That way the browser will wait for the response before closing the window/tab and it will not be cancelled.

$(window).unload(function(){
    $.ajax({
        type: 'PUT',
        url: '/your_url.php',
        async:false, //IMPORTANT, the call will be synchronous
        data: {}
    }).done(function(data) {                
        console.log('complete');
    });
});
over 4 years ago · Santiago Trujillo Relatório

0

Doing standard ajax calls in a page unload handler is being actively disabled by browsers, because waiting for it to complete delays the next thing happening in the window (for instance, loading a new page). Although you can't do PUT with it, the replacement for synchronous ajax in unload handlers is sendBeacon. sendBeacon lets you send data to your server without holding up the page you're doing it in:

window.addEventListener("unload", function() {
  navigator.sendBeacon("/log", yourDataHere);
});

The browser will send the data without preventing / delaying whatever is happening in the window (closing it, moving to a new paeg, etc.).

Beacons are POSTs, not PUTs, but other than that it's exactly what you're looking for.

Note that the unload event may not be reliable, particularly on mobile devices. You might combine the above with sending a beacon on visibilitychange as well.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda