0

I have a function that makes a check through AJAX and returns a result. The result is caught and placed within a variable, but I can't get it from inside the AJAX to show in another area. Can anyone help me please?

JS:

var get_cidade_frete = function(){
    if($('#senderCity').val() != ''){
        var frete_patinete_branco = 0;
        var frete_patinete_preto = 0;

        var frete_val_patinete_branco = $('select[name="patinete_branco_qnt"]').val();
        var frete_val_patinete_preto = $('select[name="patinete_preto_qnt"]').val();

        $.ajax({  
              type: 'POST',
              data: $('#senderCity').serialize(),
              url: './ajax/frete_calc.php',
              dataType: 'json',
               beforeSend: function(data){

               },
               success: function(data){
                    var get_frete_patinete_branco = Number(data[0]['valor']);
                    var get_frete_patinete_preto = Number(data[1]['valor']);

                    if(frete_val_patinete_branco != 0){
                        frete_patinete_branco = get_frete_patinete_branco * frete_val_patinete_branco;
                    }

                    if(frete_val_patinete_preto != 0){
                        frete_patinete_preto = get_frete_patinete_preto * frete_val_patinete_preto;
                    }

                    total_produtos_frete = frete_patinete_branco + frete_patinete_preto;

                    return total_produtos_frete;
               },
           });
    }
}

Show variable:

$('#senderCity').change(function (){
    var x = get_cidade_frete();
    console.log(x);
});
Scott
  • 1,863
  • 2
  • 24
  • 43
Red Vulpine
  • 433
  • 5
  • 17

1 Answers1

0

Pass in a callback function to the "get_cidade_frete" function, the callback then should do whatever you need to be done with the variable.

NMunro
  • 890
  • 5
  • 20