【问题标题】:postData method not executing functionpostData 方法不执行函数
【发布时间】:2012-10-31 16:18:15
【问题描述】:

我有两个 jqGrid。在第一个网格中,我选择一行,第二个网格根据第一个网格的 id 刷新数据。至少它应该是这样工作的。

//This is code from the second grid
postData: '{ lobId: ' + BudgetCore.getLobId() + ' }',

//Snippet from BudgetCore...
getLobId: function () {
    var row = jQuery(BudgetCore.GridTables.Lob).jqGrid('getGridParam', 'selrow');
    return row;
}

在 Chrome 中,我尝试调试函数 getLobid() 但它从未执行过。发送的 postData 请求:{ lobId:null }。

如果我将上面的代码更改为 '{ lobId: ' + 1 + ' }' 它可以工作,所以一定有什么错误导致这个函数无法执行。在 Chrome JS 控制台中执行 BudgetCore.getLobId() 工作正常。

【问题讨论】:

    标签: jquery asp.net jqgrid


    【解决方案1】:

    你应该使用

    postData: {
        lobId: function () {
            return $(BudgetCore.GridTables.Lob).jqGrid('getGridParam', 'selrow');
        }
    }
    

    更多详情请见the answer

    更新:如果您需要在serializeGridData 内另外使用JSON.stringify,那么您不能使用更多serializeGridData 的最简单版本:

    serializeGridData: function (postData) { return return JSON.stringify(postData); }
    

    您应该使用更复杂的serializeGridData 版本,而不是我在the answer 中描述的版本:

    serializeGridData: function (postData) {
        var propertyName, propertyValue, dataToSend = {};
        for (propertyName in postData) {
            if (postData.hasOwnProperty(propertyName)) {
                propertyValue = postData[propertyName];
                if ($.isFunction(propertyValue)) {
                    dataToSend[propertyName] = propertyValue(); // call the function
                } else {
                    dataToSend[propertyName] = propertyValue;
                }
            }
        }
        return JSON.stringify(dataToSend);
    }
    

    【讨论】:

    • 我试过这个,但是 ASP.NET asmx 要求 post 数据用 ' ' 包围。我应该在问题中提到这一点。
    • @Dan:对不起,你错了。 ASMX 接受",这是唯一允许在 JSON 中引用的字符。如果您需要将信息发送到 ASMX,您可以使用 serializeGridData 并调用 JSON.stringify,这是无法使用原始 why 函数的原因。如果您只需要使用 serializeGridData 的修改版本。对应代码见the answer
    • 我会试试这个,只需注意在同一个网格(第二个)中,我有一个带有 postData: '{ lobId: ' + BudgetCore.getLobId() + ' }' 的子网格,它工作正常。
    • @Dan:不同之处在于您每次都创建新的子网格。所以代码postData: '{ lobId: ' + BudgetCore.getLobId() + ' }' 将被执行多次次。顺便说一句,正确的代码至少是postData: JSON.stringify({lobId: BudgetCore.getLobId()})。另一方面,您创建网格一次。因此代码postData: '{ lobId: ' + BudgetCore.getLobId() + ' }' 将在jqGrid 的创建时间 期间执行一次。所有下一个请求只使用相同的字符串值postData
    猜你喜欢
    • 2017-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-11
    • 2012-06-02
    • 1970-01-01
    • 2017-08-16
    相关资源
    最近更新 更多