Skip to content

Instantly share code, notes, and snippets.

@paulofreitas
Last active December 16, 2015 10:49
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 paulofreitas/5422760 to your computer and use it in GitHub Desktop.
Save paulofreitas/5422760 to your computer and use it in GitHub Desktop.
Busca de CEP através do site dos Correios (jQuery)
/*
* Busca de CEP através do site dos Correios (jQuery)
*/
function buscaCEP(cep, callback, callbackErro) {
$.get('http://www.correios.com.br/encomendas/malote/endereco.cfm',
{'tipo': 'origem', 'cep': cep},
function (data) {
var data = $('<div/>').append(data).find('input').map(function () {
return this.value;
}).get();
callback(data[1] == '' ? false : {
'logradouro': data[0],
'bairro': data[1],
'cidade': data[2].split('/')[0].trim(),
'uf': data[2].split('/')[1]
});
}).fail(function (xhr, textStatus, errorMsg) {
callbackErro(errorMsg);
});
}
// Exemplo de uso
buscaCEP('70150900', function (dados) {
alert(JSON.stringify(dados));
});
@dinego
Copy link

dinego commented Aug 4, 2014

Não funciona mais a URL do correios, vou tentar arrumar e posto aqui a solução.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment