I am having trouble working with api in cake php I am using an api via zip to list me the city and state but and save in the bank the bank structure as table city - > name_city, state,
my code is the following sorry for the lack of organization I am still learning to use the stack
controler status:
public function add($uf = null) {
if ($uf !== null) {
$this->loadModel('Estados');
$estadosSelecionado = $this->Estados->find('list')->where(['nome' => $uf]);
if ($estadosSelecionado->count() !== 0) {
// devolver json de estados
echo json_encode($estadosSelecionado);cal
exit;
} else {
$this->Estados->newEntity();
$estado = [['nome'] => $uf];
$estados = $this->Estados->pathEntity($estado);
$this->Estados->save($estados);
echo json_encode($estados);
exit;
}
}
ajax:
function consultaCep(cep) {
$.ajax({
method: "GET",
url: 'https://viacep.com.br/ws/' + cep + '/json/',
dataType: "json",
success: function (ret) {
console.log(ret);
console.log(ret.uf);
$('#endereco').val(ret.logradouro);
$('#bairro').val(ret.bairro);
$.ajax({
method: "GET",
url: router.url + 'admin/estados/add/' + ret.uf,
dataType: "json",
success: function (set) {
Console.log('Salvo')
},
error: function (erro){
console.log('Não foi possivel salvar')
}
/* var options = [];
options.push('<option value="">Selecione um Municipio</option>');
$.each(ret, function (key, val) {
options.push('<option value="' + val.id + '">' + val.nome + '</option>');
})
$('.lista-municipios').html(options.join(' '));
$('.lista-municipios').removeAttr('disabled');
* /
});
}
/*var options = [];
options.push('<option value="">Selecione uma Cidade</option>');
$.each(ret, function (key, val) {
options.push('<option value="' + val.id + '">' + val.nome + '</option>');
})
$('.lista-municipios').html(options.join(' '));
$('.lista-municipios').removeAttr('disabled');
*/
});
}
$ (document) .ready (function () { $ ('. cepGet'). focusout (function {) { queryCep ($ (this) .val ()); if ($ ('query-municipalities')) val ()! = $ this.val (ret.uf)) {
}
});
}); / * $ (document) .ready (function () { if ($ ('query-municipalities'). val ()! = '') { console.log ($ ('list-municipalities') .prop ('disabled')); if ($ ('municipal-list'). ('disabled')) { consult municipalities ($ ('.consult-municipalities'). } } }); * /