Access varialvel after ajax request

Asked

Viewed 37 times

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 of Promise, for JQuery.ajax there is no.

  • 1

    yes @augustovasques, would be the done in place of then, 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, this return validRecVec; is executed before getting the return of ajax

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.