0
I am trying to assign a value to a variable after a request response, but my variable always returns null;
 function validaItensReceita() {
 var validRecVec;
 var ajaxValid = $('.urlCheckReceitaVeiculo').data('url');
 var placa = $('#veiculo-placa').val();
 var inscricaoMunicipal = $('#contribuintereceita-  inscricao_municipal').val();
 if (inscricaoMunicipal) {
  $.ajax({
      method: 'POST',
      url: ajaxValid + '?inscricao=' + inscricaoMunicipal,
  }).then(function (response) {
      var res = JSON.parse(response);
      if (res.status == 406) {
          validRecVec = false;
      }else{
         validRecVec = true;
       }
  });
}
 return validRecVec;
} 
I need to get a bool of this variable validRecVec
Someone correct me if I’m wrong, but this call
then(), in the format ofPromise, forJQuery.ajaxthere is no.– Augusto Vasques
yes @augustovasques, would be the
donein place ofthen, according to the documentation: https://api.jquery.com/jquery.ajax/ "but my variable always returns null" if you are returning the variable outside of the Promise treatment will return null even, thisreturn validRecVec;is executed before getting the return of ajax– Ricardo Pontual