1

这是我的javascript方法:

function AssignDebtor(e) {
        var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
        var debtorId = dataItem.Id;

        $.post({
            url: '@Url.Action("AssignDebtorToUnallocatedReceipt", "Debtor")',
            data: new { unallocatedReceiptId : cdcUnallocatedReceiptId, debtorId : debtorId },
            success: function (result, textStatus, jqXHR) {
                if (result.success) {
                    var window = $("#LookupDebtorWindow").data("kendoWindow");
                    window.close();

                    var grid = $("#UnallocatedReceiptsGrid").data("kendoGrid");
                    grid.dataSource.read();
                }
                else {
                    alert(result.error);
                }
            },
            dataType: 'json'
        });
    }

在运行时,调试器在 $.post 行停止,并返回此错误:

0x800a01bd - JavaScript 运行时错误:对象不支持此操作

deborId 成功获取它的值。我构建方法的方式可能存在问题吗?

4

2 回答 2

3
new { unallocatedReceiptId : cdcUnallocatedReceiptId, debtorId : debtorId }

看起来像一个语法错误,但不幸的是没有。相反,当您尝试将对象(甚至根本不是函数)用作构造函数时,它会抛出您遇到的异常。

只需省略new运算符

此外,正如@danludwig 所提到的,该$.post函数具有不同的签名,您不能将对象作为参数传递。而是切换到$.ajax.

于 2013-03-12T13:13:07.783 回答
2

$.post不允许您传入 javascript 对象,它需要更多强类型的方法参数。请参阅 jQuery 文档并尝试这样做:

function AssignDebtor(e) {
    var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
    var debtorId = dataItem.Id;

    $.ajax({
        type: 'POST',
        url: '@Url.Action("AssignDebtorToUnallocatedReceipt", "Debtor")',
        data: { unallocatedReceiptId : cdcUnallocatedReceiptId, debtorId : debtorId },
        success: function (result, textStatus, jqXHR) {
            if (result.success) {
                var window = $("#LookupDebtorWindow").data("kendoWindow");
                window.close();

                var grid = $("#UnallocatedReceiptsGrid").data("kendoGrid");
                grid.dataSource.read();
            }
            else {
                alert(result.error);
            }
        },
        dataType: 'json'
    });
}

...或者如果你偏爱$.post,你可以这样做:

function AssignDebtor(e) {
    var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
    var debtorId = dataItem.Id;

    $.post('@Url.Action("AssignDebtorToUnallocatedReceipt", "Debtor")',
        { unallocatedReceiptId : cdcUnallocatedReceiptId, debtorId : debtorId },
        function (result, textStatus, jqXHR) {
            if (result.success) {
                var window = $("#LookupDebtorWindow").data("kendoWindow");
                window.close();

                var grid = $("#UnallocatedReceiptsGrid").data("kendoGrid");
                grid.dataSource.read();
            }
            else {
                alert(result.error);
            }
        },
        'json'
     );
}

new请注意,我还从您的参数对象中取出了关键字。

于 2013-03-12T13:06:37.333 回答