1
I want to create a variable within the ajax and uses it in several other places, it only works when I use the async:false
and I know this is no longer recommended, how to do this properly?
follow the codes below:
JAVASCRIPT
<script type="text/javascript" src="http://code.jquery.com/jquery-2.1.0.js"></script>
<script type="text/javascript">
$(document).ready(function(e) {
// <![CDATA[
$.ajax({
url: 'lista_empresas.txt',
//async:false,
cache: false,
dataType:"json",
success: function(retorno) {
empresa = retorno['empresa']
$('body').append(empresa)
}
});
$('body').append('Minha empresa: ' + empresa)
});
// ]]>
</script>
JSON
{"company":"ATR Brasil","activity":"transport"}
you want to use the variable right after the return of the ajax? or store it in a variable to be used in the future by another function?
– Tobias Mesquita