Can conditional be used if in success in Ajax?

0

Can an if conditional be used to validate the result of a variable ?, something like:

$("#update").submit(function(e){
            e.preventDefault();
            $.ajax({
                url: "ruta/al/archivo/uploadFac.php",
                type: "POST",
                data:  new FormData(this),
                contentType: false,
                cache: false,
                processData: false,
                success: function(data){
                    if(var != 0){
                    $('#upload').modal('hide');
                    $('#Fac').modal('hide');
                    }else{
                        alert("ERROR");
                    }
                },
                error: function(data){
                    console.log(data)
                }
            });
          });
        });

Where the variable "var" is a variable that I get from the uploadFac.php script with which the request was made, can that be done?

    
asked by Fernando Garcia 29.05.2018 в 16:43
source

2 answers

0

The structure of AJAX is an object, this gives us a lot of flexibility to do and undo. What I mean is that, the node success stores an anonymous function, and behaves like any other, you can put any type of code inside. IF, WHILE, FOR , Even call another AJAX (For this it would be better to use promises and / or method .when of Jquery)

I'll give you another way of how you can build your AJAX

$(function(){
  
  var llamarAjax = function(){
    //Aquí declaras el objeto que pasarás como parámetro
    var ajax = {};
    ajax.url = 'mi_url.py';
    ajax.method = 'POST';
    ajax.data = {
      dato1: 'dato1',
      dato2: 'dato2'};
    ajax.success = function(respuesta){
      console.log(respuesta);
      //Inclusive puedes volver a invocar otro ajax
      //llamarAjax();
    };
    ajax.error = function(xhr,err){
     //Aquí mandará error por que no está permitido el uso de AJAX en snippets
      console.log(err);
    };
    
    //Aquí pasas el objeto AJAX
    $.ajax(ajax);
  }
  
  $(document).on('click','#foo',function(){
    llamarAjax();
  });


});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="button" value="Llamada!" id="foo">
    
answered by 29.05.2018 / 17:08
source
0

Of course you can, but it depends on what type of value you are going to return from the back to the front, if you are going to send an object json it would be like this:

$("#update").submit(function(e){
    e.preventDefault();
    $.ajax({
        url: "ruta/al/archivo/uploadFac.php",
        type: "POST",
        data:  new FormData(this),
        contentType: false,
        cache: false,
        processData: false,
        dataType: 'json',
        success: function(data){
            if(data.resultado != 0){
                $('#upload').modal('hide');
                $('#Fac').modal('hide');
            }else{
                alert("ERROR");
            }
        },
        error: function(data){
            console.log(data)
        }
    });
});

But if on the contrary you are going to send a string or a numerical value it would be simply with the parameter data that receives the success :

$("#update").submit(function(e){
    e.preventDefault();
    $.ajax({
        url: "ruta/al/archivo/uploadFac.php",
        type: "POST",
        data:  new FormData(this),
        contentType: false,
        cache: false,
        processData: false,
        success: function(data){
            if(data != 0){
               $('#upload').modal('hide');
               $('#Fac').modal('hide');
            }else{
               alert("ERROR");
            }
        },
        error: function(data){
            console.log(data)
        }
    });
});

Remember that the method parameter success is the one that receives the data returned by the back ..

    
answered by 29.05.2018 в 16:51