Helle there, I created a code that read a json url and return the object, but I need put this object in the variable. Here is my code:
<?php
$cep = 35400000;
?>
<script type="text/javascript">
function lerArquivo(arquivo, callback) {
var rawFile = new XMLHttpRequest();
rawFile.overrideMimeType("application/json");
rawFile.open("GET", arquivo, true);
rawFile.onreadystatechange = function() {
if (rawFile.readyState === 4 && rawFile.status == "200") {
callback(rawFile.responseText);
}
}
return rawFile.send(null);
}
var url = "https://viacep.com.br/ws/" + "<?=$cep?>" + "/json/";
var cidade = lerArquivo(url, function(texto){
var dado = JSON.parse(texto);
});
document.write(cidade); //It does not working!
</script>
I can not use a ajax. Someone can help? Because I need use this variable.