javascript – $.get callback is not called and I can't debug to find out why

Question:

I'm developing a mobile application (with cord) that needs to make a select in the online BD, and with this data update the cell's database, but I came across a problem, I perform the select in the cell's BD then I perform a

$.get(url, parametro, fuction(data){
});

to select the register in the online Bank, and inside I check if the data is different and if it is I do the update, but when I do a Debug it doesn't enter my get I'm told this happens because it's an ASYNCHRONOUS function but I don't know how to solve this.

My code:

    queryDB: function (tx) {
        tx.executeSql('SELECT * FROM ouvidoria11', [], app.querySuccess3, app.errorCB);
    },
querySuccess3: function (tx, results) {
        var len = results.rows.length;
        var protocolo;
        var tblText = '';
        if (len < 1) {
            alert("Não existe nenhum cadastro!");
        }
        else {
            for (var i = 0; i < len; i++) {
                if (results.rows.item(i).protocolo == null) {
                    app.protocoloSolicitacao(results.rows.item(i).id, results.rows.item(i).assunto, results.rows.item(i).mensagem, results.rows.item(i).endereco, results.rows.item(i).anexo);
                }
                tblText += '<span onclick="app.guardaVariavel('+results.rows.item(i).protocolo+')"><table id="t01" class="table-bordered">';
                tblText += '<tr><th>Protocolo</th><td>' + results.rows.item(i).protocolo + '</td></tr>';
                tblText += '<tr><th>Assunto</th><td>' + results.rows.item(i).assunto + '</td></tr>';
                tblText += '<tr><th>Mensagem</th><td>' + results.rows.item(i).mensagem + '</td></tr>';
                tblText += '<tr><th>Endereco</th><td>' + results.rows.item(i).endereco + '</td></tr>';
                tblText += '<tr><th>Anexos</th><td>' + results.rows.item(i).anexo + ' </td></tr>';
                tblText += '<tr><th>Prazo</th><td>' + results.rows.item(i).prazo + '</td></tr>';
                tblText += '<tr><th>Status</th><td>' + results.rows.item(i).status + '</td></tr>';
                tblText += '<tr><th>Departamento</th><td>' + results.rows.item(i).nomeDepartamento + '</td></tr>';
                tblText += '</table></span>';
                document.getElementById("tabelaSolicitacao").innerHTML = 

tblText;
                    var protocolo = results.rows.item(i).protocolo;
                    var nomeDepartamento = results.rows.item(i).nomeDepartamento;
                    var prazo = results.rows.item(i).prazo;
                    var statusBD = results.rows.item(i).status;
                    app.atualizarTabela(prazo, statusBD, nomeDepartamento, protocolo);//Chama a função para atualizar os dados
                }
            }
        },
atualizarTabela: function(prazo, statusBD, nomeDepartamento, protocolo){    
        var serviceURL = "http://ouvidoria.azurewebsites.net/Solicitacao/SelectAtualizacao";
        var parametros = { id: protocolo }
        $.get(serviceURL, parametros, function (data) {
            alert("Teste Get");
            if (data[0].meuPrazo != prazo || data[0].status != statusBD || data[0].nomeDepartamento != nomeDepartamento) {
                db.transaction(update, null, null);
                function update(tx) {
                    tx.executeSql('UPDATE ouvidoria11 set prazo = "' + prazo + '", status = "' + statusBD + '", nomeDepartamento = "' + nomeDepartamento + '" WHERE protocolo="' + protocolo + '"');
                }
            }
        });
    },

Obs.: As said, it calls the atualizarTabela() function, however it does not enter $.get , nor does it display the alert(''); It displays the alert() after it ends for the function querySucess3() give it displays the several times, exactly the number of times the function atualizarTabela() was called in for the main function querySucess3()

meuPrazo is returning of undefined when I set to debug console.log(data) it returns the following:

Object
abort: function ( statusText ) {
always: function () {
complete: function () {
done: function () {
error: function () {
fail: function () {
getAllResponseHeaders: function () {
getResponseHeader: function ( key ) {
overrideMimeType: function ( type ) {
pipe: function ( /* fnDone, fnFail, fnProgress */ ) {
progress: function () {
promise: function ( obj ) {
readyState: 4
responseJSON: Array[1]
responseText: "[{"id":0,"assunto":null,"mensagem":null,"endereco":null,"anexo":null,"status":"Aguardando","meuPrazo":"Indefinido","nomeDepartamento":null,"id_departamento":2}]"
setRequestHeader: function ( name, value ) {
state: function () {
status: 200
statusCode: function ( map ) {
statusText: "OK"
success: function () {
then: function ( /* fnDone, fnFail, fnProgress */ ) {
__proto__: Object

Answer:

Pass the async:false parameter in ajax should resolve, I'll demonstrate an example:

$.ajax({type:"GET", url:view, dataType:"json", data:data,async:false,              // FAZ UM AJAX SINCRONIZADO COM A FUNCAO
        success: function(json) { retorno = json; },                                    // RETORNO DO AJAX NO SUCCESS
        error: function(json) { retorno=json; },                                        // RETORNO DO AJAX NO ERROR
        beforeSend: function() { $('body').css('cursor','wait'); },                     // RETORNO DO AJAX NO ERROR
        complete: function(){ $('body').css('cursor','default'); }
    }); 

There's such a question here https://stackoverflow.com/questions/133310/how-can-i-get-jquery-to-perform-a-synchronous-rather-than-asynchronous-ajax-re

EDIT: in your code above, it would be like this to know if any return value is arriving:

$.ajax({type:"GET",url:serviceURL,data:parametros, async:false,
    complete: function (data) {
        console.log(data);
    }
});
Scroll to Top