Skip to content

Instantly share code, notes, and snippets.

@merciof
Last active November 30, 2020 21:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save merciof/1034541e7142fcea5a81993a26551810 to your computer and use it in GitHub Desktop.
Save merciof/1034541e7142fcea5a81993a26551810 to your computer and use it in GitHub Desktop.
Consumo de webservice viacep com jquery
<html>
<head></head>
<body>
<form action="">
<input type="text" name="cep" placeholder="cep">
<input type="text" name="rua" placeholder="rua">
<input type="text" name="numero" placeholder="numero">
<input type="text" name="cidade" placeholder="cidade">
<input type="text" name="estado" placeholder="estado" size="2">
<input type="text" name="bairro" placeholder="bairro">
</form>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"
integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ=" crossorigin="anonymous"></script>
<script>
$('input[name="cep"]').blur(function (e) {
let url = 'http://viacep.com.br/ws/' + $('input[name="cep"]').val() + '/json';
$.getJSON( url, function ( $dado ) {
//$('body').html($dado.bairro);
$('input[name="rua"]').val($dado.logradouro);
$('input[name="bairro"]').val($dado.bairro);
$('input[name="cidade"]').val($dado.localidade);
$('input[name="estado"]').val($dado.uf);
});
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment