8
I am using a webservice (viaCep) for automatic filling of the backyard, for when the user type a zip code. The webservice returns me a json with the information from the patio, and from that return I fill the inputs (Street, city, etc..).
When I manually type the information of the address fields, I can get it in the request.
When fields are filled in with information from the Webservice, when retrieving the request from the form, the fields (City, State, Street, etc.) arrive null.
Javascript that returns the json webservice:
function limpa_formulário_cep() {
document.getElementById('endereco').value = ("");
document.getElementById('bairro').value = ("");
document.getElementById('cidade').value = ("");
document.getElementById('estado').value = ("");
}
function meu_callback(conteudo) {
if (!("erro" in conteudo)) {
document.getElementById('endereco').value = (conteudo.logradouro);
document.getElementById('bairro').value = (conteudo.bairro);
document.getElementById('cidade').value = (conteudo.localidade);
document.getElementById('estado').value = (conteudo.uf)
}
else {
limpa_formulário_cep();
alert("CEP não encontrado.");
}
}
function pesquisacep(valor) {
var cep = valor.replace(/\D/g, '');
if (cep != "") {
var validacep = /^[0-9]{8}$/;
if (validacep.test(cep)) {
document.getElementById('endereco').value = "...";
document.getElementById('bairro').value = "...";
document.getElementById('cidade').value = "...";
document.getElementById('estado').value = "...";
var script = document.createElement('script');
script.src = 'https://viacep.com.br/ws/' + cep + '/json/?callback=meu_callback';
document.body.appendChild(script);
}
else {
limpa_formulário_cep();
alert("Formato de CEP inválido.");
}
}
else {
limpa_formulário_cep();
}
};
Form what queries and receives the return of the JS:
<input name="cep" id="cep" onblur="pesquisacep(this.value);" ng-model="contribuinte.cep" type="text"/>
<input name="estado" id="estado" ng-model="contribuinte.estado" charcase="uppercase" type="text"/>
<input name="cidade" id="cidade" ng-model="contribuinte.cidade" type="text"/>
From what I understand you are using Angularjs along with pure Javascript, correct?
– Camilo Silva
Yeah, @Camilosilva . Version 1x.
– Vinícius
Yes, my controller receives this 'contributor' object'.
– Vinícius
I honestly did not understand the problem, I found the statement confused.
– WoF_Angel
Excuse the sincerity, but I recommend adjusting this Portuguese there, at least for me the question remains meaningless. You said, "On it I fill out some inputs from my form. As an example, returned "Rio de Janeiro" and filled my form the input label 'City'". It means it returns something to you before you type???
– WoF_Angel
Has any response helped solve the problem and can address similar questions from other users? If so, make sure to mark the answer as accepted. To do this just click on the left side of it (below the indicator of up and down votes).
– Sorack