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 needsresult
must be within that functionsuccess
. 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– Sergio
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.]
– Silas Sena
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
– Sergio