2013-03-12 93 views
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' 
     }); 
    } 

在運行時,調試器停止在$。員額線,並返回此錯誤:

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

回答

3
new { unallocatedReceiptId : cdcUnallocatedReceiptId, debtorId : debtorId } 

看起來像一個語法錯誤,但沒有遺憾。相反,當你嘗試使用一個對象(甚至根本不是函數)作爲構造函數時,它會拋出異常。

只需省略new operator即可。

此外,正如@danludwig所提到的,$.post function具有不同的簽名,您不能傳入一個對象作爲參數。而是切換到$.ajax

2

$.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

相關問題