调用C#COM组件时在javascript中键入mis match error
我用C#创建了一个Com组件,并尝试用Javascript访问它 我的C#方法是调用C#COM组件时在javascript中键入mis match error,c#,javascript,html,c#-4.0,com,C#,Javascript,Html,C# 4.0,Com,我用C#创建了一个Com组件,并尝试用Javascript访问它 我的C#方法是 Class myComComponent { private int[] nAllData; public int[] GetArray(int index) { //Some Logic here that will return integer type of array{1,12,15,48,1452,45}
Class myComComponent
{
private int[] nAllData;
public int[] GetArray(int index)
{
//Some Logic here that will return integer type of array{1,12,15,48,1452,45}
return nAllData;
}
}
从javascript调用它,但它给我一个类型不匹配错误
Javascript代码
function MyComComponent_onload() {
try {
var nAllData = new Array();
for (var i = 0; i<= 5; i++)
{
nAllData.push(myComComponent.GetArray(i));
}
}
catch (err)
{
alert(err.message);
}
}
<html>
<head>
<object id="myComComponent" name="myComComponent" classid="clsid:4794D615-BE51-4A1E-B1BA-453F6E9337C4">
</head>
<body onload="MyComComponent_onload();">
//// Html Code goes here
</body>
<html>
函数MyComComponent_onload(){
试一试{
var nAllData=新数组();
对于(var i=0;iJavaScript只能使用COM的自动化兼容子集。整数数组不是该子集的一部分
您需要返回一个与JavaScript兼容的SAFEARRAY变量,或者返回一个带有IEnumVariant接口的对象(因此foreach工作),以及带有索引默认属性的IDispatch(因此使用方括号索引工作).JavaScript只能使用COM的自动化兼容子集。整数数组不属于该子集
您需要返回一个与JavaScript兼容的SAFEARRAY变量,或者返回一个带有IEnumVariant接口的对象(因此foreach工作),以及带有索引默认属性的IDispatch(因此使用方括号索引工作).你的c#函数返回一个整数数组,所以你需要在javascript中使用一个2D数组来存储这些数组……我说得对吗?@Giwrgos Tsopanoglou我怎么能做到这一点。我不知道如何转换它。你能不能简单地告诉我,我从来没有将c#和javascript结合在一起过,而且我不是专家。也许你可以尝试添加“nAllData[I]=new array()”在for循环中。你的c#函数返回一个整数数组,所以你的javascript中需要一个2D数组来存储这些数组……我说得对吗?@Giwrgos Tsopanoglou我怎么能做到这一点。我不知道如何转换它。你能不能简单地告诉我,我从来没有将c#和javascript组合在一起,我不是专家。也许你可以尝试添加“nAllData[I]=new Array()“在for循环中。我认为你的方法是正确的。你能给我一个更好理解的例子吗?我认为你的方法是正确的。你能给我一个更好理解的例子吗