0

I have a classes that,

$.row = {
    rowJson:"1",
    getRow: function (rowId) {
        $.ajax({
            type: "POST",
            url: "bla bla post link",
            data: "{bla bala data}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (dc, status) { $.row.rowJson = JSON.parse(dc.d);},
            error: function (request, status) { $.row.rowJson = "-1"; alert(request.responseText) }
        });
    },
    success: function () { if (rowJson == "-1") { return false; } else { return true; } },
    Id: function () { return rowJson[0].Id;},
    Uygulama: function () { return rowJson[0].Uygulama;},
    Kullanici: function () { return rowJson[0].Kullanici;},
    Seviye: function () { return rowJson[0].Seviye;},
    Durum: function () { return rowJson[0].Durum;},
    Baslik: function () { return rowJson[0].Baslik;},
    TarihSaat: function () { return rowJson[0].TarihSaat;},
    Aciklama: function () { return rowJson[0].Aciklama;},
    TalepDurumId: function () { return rowJson[0].TalepDurumId;},
    UygulamaId: function () { return rowJson[0].UygulamaId;},
    TalepSeviyeId: function () { return rowJson[0].TalepSeviyeId;},
}

after I called like this way,

function _rowInfo(rowId) {
    pWaitShow("Please Wait");
    $.row.getRow(rowId);
    if($.row.success())
    {
        pWaitHide();
        console.log([kbJson,$.row.rowJson])
    }
    else
    {
        pWaitHide();
        $.ms.standartError();
    }
}

Console value is undefined when I called this function (_rowInfo(5)) firstly but console value is real object when I called this function (_rowInfo(5)) after that firstly called I mean say secondly, thirdly

István
  • 5,057
  • 10
  • 38
  • 67
Hüseyin Okumuş
  • 157
  • 1
  • 3
  • 17
  • Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Andreas Jun 30 '16 at 11:35
  • I can understand @andreas .Sorry,How I call that ? – Hüseyin Okumuş Jun 30 '16 at 11:37

1 Answers1

0

The response comes from an asynchronous call, so you should do something like this to handle this behavior :

You can change the getRow function like that :

 getRow: function(rowId, callback) {   
   $.ajax({
     type: "POST",
     url: "bla bla post link",
     data: "{bla bala data}",
     contentType: "application/json; charset=utf-8",
     dataType: "json",
     success: function(dc, status) {
       rowJson = JSON.parse(dc.d);
       callback(rowJson);
     },
     error: function(request, status) {
       rowJson = "-1";
       alert(request.responseText)
       callback(rowJson);
     }
   });
 },

And change your _rowInfo to :

 function _rowInfo(rowId) {
   pWaitShow("Please Wait");
   $.row.getRow(rowId, function(row) {
     if($.row.success())
       pWaitHide();
       console.log([kbJson, row])
     } else {
       pWaitHide();
       $.ms.standartError();
     }
   });
 }
Quentin Roger
  • 6,410
  • 2
  • 23
  • 36