Last active
December 16, 2015 10:49
-
-
Save paulofreitas/5422760 to your computer and use it in GitHub Desktop.
Busca de CEP através do site dos Correios (jQuery)
This file contains 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
/* | |
* 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)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Não funciona mais a URL do correios, vou tentar arrumar e posto aqui a solução.