Display success message after registration is completed via AJAX

1

I want to display a message as "loading" while processing the registration and another with "Registration complete", this is my AJAX code

$.ajax({
    type: "POST",
    url: "http://localhost:5001/v1/enterprise",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    data: JSON.stringify(data),
    crossDomain: true,
    success: function(data) {

        guid = data;


        $(".cad_sucesso").css({
            display: 'block'
        })

        setTimeout(function() {
            $(".cad_sucesso").fadeOut();
            $(".processo_sucesso").css({
                display: 'block'
            })
            setTimeout(function() {
                window.location.href = "/dataImmobile/DataImmobile/NewImmobile";
            }, 1000);
        }, 3000);

    },
    failure: function(data) {
        alert(data.responseText);
    },
    error: function(data) {
        alert(data.responseText);
    }
});

What am I doing here? By first displaying a div with a 'loading' message, and after a few seconds it displays the div with the 'cadastro complete' message, but it is not dynamic since I am setting the time to display the messages. How do I take the time for this requisition?

    
asked by anonymous 12.04.2018 / 15:59

1 answer

0

You can use the beforeSend event to display your progress div and hide it in success. Instead of .css({display: 'block'); you could simply use the show(); method

$.ajax({
  type: "POST",
  url: "http://localhost:5001/v1/enterprise",
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  data: JSON.stringify(data),
  crossDomain: true,
  beforeSend: function() {
    //Exibe a div de progresso
    $(".cad_sucesso").show();
    
  },
  success: function(data) {

    guid = data;

    $(".cad_sucesso").fadeOut(function() {
      //exibe a div do sucesso após o fadeout do progresso
      $(".processo_sucesso").show();
      
      setTimeout(function() {
        window.location.href = "/dataImmobile/DataImmobile/NewImmobile";
      }, 1000);

    });

  },
  failure: function(data) {
    alert(data.responseText);
  },
  error: function(data) {
    alert(data.responseText);
  }
});
    
13.04.2018 / 16:16