Variable value Undefined js

Asked

Viewed 392 times

4

I am developing a mobile application with Cordova, in the mobile version there is a BD that keeps checking if there is any change in online BD, if there is it updates in mobile follows below my code:

var serviceURL = "http://projeto.websites.net/Verifica/Atualiza";
var parametros = { id: protocolo }
$.ajax({
        type: "GET", url: serviceURL, data: parametros, async: false,
        complete: function (data) {
            console.log(data);
            if (data.prazo != meuprazo || data.status != meustatusBD || data.id_Departamento != meunomeDepartamento) {             
                db.transaction(select, errorCB, null);
                function select(tx) {
                    tx.executeSql('UPDATE ouvidoria11 set prazo = "' + data.prazo + '", status = "' + data.status + '", nomeDepartamento = "' + data.id_Departamento + '" WHERE protocolo="' + protocolo + '"');
                }
                function errorCB() {
                    alert("ERROCB");
                }

            }
        }
    });

However the value that is returned from date.term is as undefined, put a console.log(data) to check what was happening and realized that he is yes receiving the values:

Object {readyState: 4, getResponseHeader: function, getAllResponseHeaders: 
function, setRequestHeader: function, overrideMimeType: function…}
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]
       0: Object
          anexo: null
          assunto: null
          endereco: null
          id: 0
          id_departamento: 2
          mensagem: null
          nomeDepartamento: null
          prazo: "Indefinido"
          status: "Aguardando"
       __proto__: Object
       length: 1
__proto__: Array[0]
responseText: "[{"id":0,"assunto":null,"mensagem":null,"endereco":null,"anexo":null,"status":"Aguardando","prazo":"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

It’s wrong the way I’m trying to receive these values?

  • You don’t want to use success instead of complete?

  • You managed to solve this problem?

2 answers

1

Correcting: receive this parameter in this way:

console.log(JSON.parse(data.responseText));

1

I see two things you can change.

When you use the complete the jQuery will pass as first argument the object jqXHR. Then you need to go get data.responseText. If you use the callback success instead of complete it will give the data that ajax returns in the first argument of the function.

In your console.log I see that the data is in JSON, so you also need to parse them, or specify in ajax dataType: 'json'.

I suggest you use it like this:

$.ajax({
    type: "GET",
    url: serviceURL,
    data: parametros,
    async: false, // isto não é nada bom!
    success: function (data) {
        data = JSON.parse(data); // ou juntares em cima: "dataType='json',"
        console.log(data);
        if (data.prazo != meuprazo || data.status != meustatusBD || data.id_Departamento != meunomeDepartamento) {             
            db.transaction(select, errorCB, null);
            function select(tx) {
                tx.executeSql('UPDATE ouvidoria11 set prazo = "' + data.prazo + '", status = "' + data.status + '", nomeDepartamento = "' + data.id_Departamento + '" WHERE protocolo="' + protocolo + '"');
            }
            function errorCB() {
                alert("ERROCB");
            }

        }
    }
});

I see you still have async: false. That’s not good. If you explain why you have it I can help change the logic so you don’t use it like this.

  • Thank you so much for the help but you are giving an error in the data line = JSON.parse(data); the error is this 'Uncaught Syntaxerror: Unexpected token o'

  • @Viniciusvaz ok, say what comes console.log(typeof data);? are using Success or complete?

  • I’m using Success, he got a return from 'Object'

  • @Viniciusvaz so he’s already parsing the JSON. You can take the line data = JSON.parse(data); and should already work.

  • Now it worked just had to put date[0]. term, on async: false, was a solution I passed right here in stackoverflow if I’m wrong I ask you to help me the question is here:(http://answall.com/questions/72252/d%C3%Bavidas-sobre-fun%C3%A7%C3%A3o-assincrona-javascript/72412? noredirect=1#comment149000_72412)

  • @Viniciusvaz if you want you can mark the answer as accepted

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.