Javascript function returns Undefined

Asked

Viewed 134 times

0

Hello !

I have a Javascript function where I use ajax , I need the return of this function to be the data that is returned by ajax. I can get the return smoothly but I can’t assign the return value to a variable to return to the function. The function is always returning Undefined, someone can help me ???

function verifica_cep_cliente(){

            var var_endereco_selecionado = $('#endereco_entrega_utilizar').val();
            var cliente_selecionado = '<?php echo $this->session->cliente_externo_logado ?>';
            var var_forma_entrega = '<?php echo $this->session->FORMAENTREGA_CODIGO ?>';
            var retorno;

            $.ajax({
               url: '<?php echo base_url('Pagamento/verifica_cep_cliente_ajax') ?>',
               type: 'POST',
               dataType: 'TEXT',
               data:"endereco_entrega="+var_endereco_selecionado +"&cliente="+ cliente_selecionado+"&formaentrega="+var_forma_entrega,
               success: function (result){
                   //return result; 
                   retorno = result;
               }
            }); 

           //return retorno;


           alert("retorno " . retorno);


        }
  • That one alert("retorno " . retorno); or the code that needs result must be within that function success. She’ll run when the server responds and there’s no way to access the code outside of her. Another duplicate of the same problem is: http://answall.com/a/63605/129, and there are more

  • Um, ok. What I needed was to access the ajax value for the variable that is in the Js function. But I’ve solved it otherwise, thanks for the help.]

  • In this case it calls the function that needs the variable from within the ajax response. So the function is called as soon as Javascript knows the server response

No answers

Browser other questions tagged

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