Send data only when I relocate it

1

I have a link with your respective href that wants to send data to the server but I use prevent to ask if I'm sure, if I give it IF I thought that I should send the communication with the server but not do anything, but if I relocate it works

$(function() {
	$('.envios').click(function(e) {
		e.preventDefault();
		swal({
			title: "Aviso",
			text: "Está seguro que desea salir ? ",
			icon: "warning",
			buttons: true,
			dangerMode: true,
		})
		.then((willDelete) => {
			if (willDelete) {
            //Mensaje Opcional
            swal("Usted será redireccionado", {
            	icon: "success",
            });
            console.log(this.href);
            // redirección location.href = this.href
        } else {
        	swal("Canceceló la acción");
        }
    });
	});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
<a href='envaluacion/primera' class="envios">Irse</a>
<a href='envaluacion/segunda' class="envios">Irse a Otra</a>

when I use ajax, it redirects me, but why and how do I fix it?

$('.btnDeletes').on('click', function(e) {
      e.preventDefault();
      var nombreEva = 'evaDinamica';
      var idSalida = this.id;
      console.log('LÑ', idSalida);

      swal({
          title: "Esta seguro de eliminar al usuario?",
          text: "Elija una opcion",
          icon: "warning",
          buttons: true,
          dangerMode: true,
        }).then((willDelete) => {
            if (willDelete) {
              $.ajax({
                url: "/evaluacion/concerderRsol/" + nombreEva + "/" + id,
                type: "GET",
                data: {
                  nombreEva: nombreEva,
                  user: id
                },
                dataType: "html",
                success: function() {
                  console.log('success');
                  swal("si!", "bien", "success");
                },
                error: function(xhr, ajaxOptions, thrownError) {
                  console.log('error');
                  swal("Error!", "Please try again", "error");
                }
              });
            }
          }
        });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>


<button class="btnDeletes" id="uno">enviar</button>
    
asked by hubman 15.08.2018 в 06:17
source

1 answer

1
  

I thought I should send the communication to the server but not   do nothing

No, doing click a way that the request would occur would be, through redirection, or through Ajax to avoid having to reload the page. (this request was missing in its first code portion)

For the second example, as I mentioned, the error is simple and basically is a syntax error, since it is necessary to close the then with the parentheses ) , in addition to recommending using done for the return of success of requests Ajax .

Possible final code

$('.btnDeletes').on('click', function(e) {
    e.preventDefault();
    var nombreEva = 'evaDinamica';
    var idSalida = this.id;
    let id = 12; // valor de prueba
    swal({
      title: "Esta seguro de eliminar al usuario?",
      text: "Elija una opcion",
      icon: "warning",
      buttons: true,
      dangerMode: true,
    }).then((willDelete) => {
      if (willDelete) {
        $.ajax({
          url: "/evaluacion/concerderRsol/" + nombreEva + "/" + id,
          type: "GET",
          dataType: "html"
        }).done(function(data){
            swal("si!", "bien", "success");
        });
      }
    }); //<- hacía falta
});
    
answered by 15.08.2018 / 07:44
source