Variable value undefined js

2

I'm developing a mobile application with the cord, in the mobile version there is a BD that is checking if there is any change in the BD online, if there is an update on the mobile phone, follow my code below:

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");
                }

            }
        }
    });

But the value that is returned from date.prazo is as undefined , I put console.log(data) to check what was happening and I realized that it is yes getting 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

Is the way I'm trying to get these values wrong?

    
asked by anonymous 07.07.2015 / 15:08

2 answers

0

I see two things you can change.

When you use complete jQuery will pass as the first argument the jqXHR object. In this case you need to get data.responseText . If the callback uses 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 using 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 also see that you have async: false . This is not good. If you explain why you have it I can help change the logic so you do not use it like this.

    
07.07.2015 / 15:20
0

Correcting: receive this parameter as follows:

console.log(JSON.parse(data.responseText));
    
07.07.2015 / 15:14