获取对象不支持此操作错误在$。员额

问题描述:

这里是我的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' 
     }); 
    } 

在运行时,调试器停止在$。员额线,并返回此错误:

0x800a01bd - JavaScript runtime error: Object doesn't support this action

debtorId成功获得其价值。在我构建该方法的过程中,可能存在问题吗?

+0

你有jQuery链接? – 2013-03-12 11:59:11

+0

它发生在你的成功函数内部,还是仅仅是发布声明本身? – mattytommo 2013-03-12 12:10:16

+0

@JohnEcho是的jquery是链接的,因为代码肯定不会得到尽可能没有jquery(使用Kendo UI - 这种方法是什么得到执行后点击自定义网格命令按钮) – 2013-03-12 12:21:14

new { unallocatedReceiptId : cdcUnallocatedReceiptId, debtorId : debtorId } 

看起来像一个语法错误,但没有遗憾。相反,当你尝试使用一个对象(甚至根本不是函数)作为构造函数时,它会抛出异常。

只需省略new operator即可。

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

$.post不允许你传入JavaScript对象,它需要更强类型的方法参数。 See the jQuery docs而是试试这个:

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关键字从参数对象。

+0

没有对象有名字,都是“匿名”的。你在代码中改变了什么? – Bergi 2013-03-12 13:11:42

+1

@Bergi好吧我不会争辩,我删除了'匿名',并取而代之'JavaScript对象'。我将它从'$ .post'改为'$ .ajax'。还有一个替代方法,使用'$ .post'和单独的方法函数参数。 – danludwig 2013-03-12 13:15:03