Jquery对象“undefined”首先调用

时间:2016-06-30 11:33:17

标签: javascript jquery

我有一个课程,

$.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;},
}
在我这样打电话之后

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

首次调用此函数(undefined)时,控制台值为_rowInfo(5) 但是当我调用这个函数(_rowInfo(5))后,控制台值是真实的对象,之后我称其为第二个,第三个是

1 个答案:

答案 0 :(得分:0)

响应来自异步调用,因此您应该执行以下操作来处理此行为:

您可以像这样更改getRow功能:

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

将您的_rowInfo更改为:

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