1
I don’t know how to solve the problem of Access-Control-Allow-Origin
. What to do to function properly?
function calc() {
var cepDestino = document.getElementById( 'cepDestino' ).value;
if(cepDestino.trim() != ""){
var request = new XMLHttpRequest();
request.open('GET', 'http://cep.republicavirtual.com.br/web_cep.php?formato=javascript&cep='+cepDestino, true);
request.onload = function() {
if (request.status >= 200 && request.status < 400) {
var data = JSON.parse(request.responseText);
document.getElementById('bairro').value(data.bairro);
document.getElementById('cidade').value(data.cidade);
document.getElementById('estado').value(data.estado);
} else {
// Não deu certo
}
};
request.onerror = function() {
// A conexão nem sequer deu certo
};
request.send();
}
}
<p>
<label for="cepDestino">CEP Destino</label>
<input class="input" type="text" name="cepDestino" id="cepDestino" >
<div id="bairro"></div> - <div id="cidade"></div> - <div id="estado"></div>
</p>
<p>
<input class="button" type="button" id="calcular" value="Calcular" onclick="calc();">
</p>
@Pedrocamarajunior I am not using jquery. There is nothing to do with my existing problem with this.
– Tiago
Sorry, I asked the wrong question: Related duplicate http://answall.com/questions/3183/requisi%C3%A7%C3%A3o-ajax-cross-Domain-with-javascript-neat-without-Apis
– Pedro Camara Junior