获取对象不支持此操作错误在$。员额
问题描述:
这里是我的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成功获得其价值。在我构建该方法的过程中,可能存在问题吗?
答
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
关键字从参数对象。
你有jQuery链接? – 2013-03-12 11:59:11
它发生在你的成功函数内部,还是仅仅是发布声明本身? – mattytommo 2013-03-12 12:10:16
@JohnEcho是的jquery是链接的,因为代码肯定不会得到尽可能没有jquery(使用Kendo UI - 这种方法是什么得到执行后点击自定义网格命令按钮) – 2013-03-12 12:21:14