0
I have a mobile application in development using Cordova
, that performs a select on my external BD and checks with mobile data so knowing if there is any change in data.
I’m using a request via AJAX
. The correct operation would be, the app checks if there is change if you have something different it runs a UPDATE
on my mobile BD, then it would have to assemble a table with the information for the user to view.
But he does not expect the return of AJAX
to find out if there are changes, it simply calls the function and mounts the table. When I inspect line by line it even waits for the return of AJAX, but does not let the call of the function to do the UPDATE
in BD mobile.
Follow my code below:
atualizarTabela: function (tx, results) {
var len = results.rows.length;
if (conectado == 1) { Verificação se existe conexão com internet
for (var cont = 0; cont < len; cont++) {
var prazo = results.rows.item(cont).prazo;
var statusBD = results.rows.item(cont).status;
var nomeDepartamento = results.rows.item(cont).nomeDepartamento;
var protocolo = results.rows.item(cont).protocolo;
var serviceURL = "http://192.168.0.104:18376/Solicitacao/SelectAtualizacao";
var parametros = { id: protocolo }
$.ajax({
type: "GET",
url: serviceURL,
data: parametros,
async: false, // Tentei colocar async false mas não resolveu
success: function (data) {
alert("Retorno Ajax ok");
if (data[0].prazo != prazo || data[0].status != statusBD || data[0].id_departamento != nomeDepartamento) { //Verifica se existe alterações
db.transaction(select, errorCB, sucess);
function select(tx) { //Este seria o meu Update no bd local
tx.executeSql('UPDATE ouvidoria11 set prazo = "' + data[0].prazo + '", status = "' + data[0].status + '", nomeDepartamento = "' + data[0].nomeDepartamento + '" WHERE protocolo="' + protocolo + '"');
app.selectDB();
}
function errorCB() {
alert("ERROCB");
}
function sucess() {
alert("Atualizado!");
}
}
}
});
}
app.montarSolicitacao(tx, results);
}
else { //Caso não tenha internet, monta tabela sem verificação de Atualização
app.montarSolicitacao(tx, results);
}
},
As @Tobymosque said the excerpt
app.montarSolicitacao(tx, results);
must be within the methodsuccess
from AJAX, I believe after the linedb.transaction(select, errorCB, sucess);
.– KaduAmaral