Last active
November 30, 2020 21:53
-
-
Save duzitug/1034541e7142fcea5a81993a26551810 to your computer and use it in GitHub Desktop.
Consumo de webservice viacep com jquery
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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