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

259
Visualizações
How to show error if ajax response is not returned after specfic time

How can i make sure that Ajax response was not sent because of some error? as the loader keeps loading,how can i make sure to display some message after specific time to user that something went wrong ? This is what i am trying.

  jQuery.ajax({

        type: "POST",
        url:url,
        data:{id:id},

        beforeSend:function() {
            //loader()
        },
        success: function(msg) {

           // sucess
        }
    });
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

jQuery.ajax({

        type: "POST",
        url:url,
        data:{id:id},

        beforeSend:function() {
            //loader()
        },
        success: function(msg) {

           // sucess
        },
        error: function (jqXHR, exception) {
            var msg = '';
            if (jqXHR.status === 0) {
                msg = 'Not connect.\n Verify Network.';
            } else if (jqXHR.status == 404) {
                msg = 'Requested page not found. [404]';
            } else if (jqXHR.status == 500) {
                msg = 'Internal Server Error [500].';
            } else if (exception === 'parsererror') {
                msg = 'Requested JSON parse failed.';
            } else if (exception === 'timeout') {
                msg = 'Time out error.';
            } else if (exception === 'abort') {
                msg = 'Ajax request aborted.';
            } else {
                msg = 'Uncaught Error.\n' + jqXHR.responseText;
            }
        },
        timeout: 5000 // sets timeout to 3 seconds
    });

over 4 years ago · Santiago Trujillo Relatório

0

Try this code

jQuery.ajax({
        type: "POST",
        url:url,
        data:{id:id,type:type},
        timeout:5000 //This will set timeout to 5 seconds
        beforeSend:function() {
             // loader
        },
        success: function(msg) {

          // Success

        },
        error: function(){
           // It will fire once timeout is reached
           // If you have any other error in the code
           // like wrong function name etc,keep it mind timeout 
           // function wouldn't work,and you will see the error without 
           // waiting for the timeout function.

        })};

By the way you can get list of all the error as mentioned by @alok.

over 4 years ago · Santiago Trujillo Relatório

0

You can set time out option handle error in the error callback.

$.ajax({
url: "test.html",
error: function(){
    // will fire when timeout is reached
},
success: function(){
    //do something
},
timeout: 3000 // sets timeout to 3 seconds
});

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