将数组从C#COM对象传递到JavaScript?
与此类似,但使用C# 我有一个COM控件,它将字符串数组传回javascript。似乎javascript无法理解我返回的内容,javascript中的数组总是未定义的 Javascript:将数组从C#COM对象传递到JavaScript?,c#,javascript,arrays,com,activex,C#,Javascript,Arrays,Com,Activex,与此类似,但使用C# 我有一个COM控件,它将字符串数组传回javascript。似乎javascript无法理解我返回的内容,javascript中的数组总是未定义的 Javascript: try { keystore.openKeyStore("MY", true, false); var fNames = new Array(); fNames = keystore.getAllFriendlyNames(); document.getElementById('par'
try
{
keystore.openKeyStore("MY", true, false);
var fNames = new Array();
fNames = keystore.getAllFriendlyNames();
document.getElementById('par').innerHTML = fNames[0];
}
catch(err)
{
document.getElementById('err').innerHTML = err.description;
}
输出fNames[0]的“未定义”代码>
C#:
public对象[]getAllFriendlyNames()
{
如果(!keystoreInitialized)
抛出新异常(“密钥存储尚未初始化”);
X509Certificate2Collection allCerts=证书存储证书;
int storeSize=allCerts.Count;
如果(storeSize==0)
抛出新异常(“空密钥存储,可能使用错误的密钥存储名称打开”);
object[]friendlyNames=新对象[storeSize];
对于(int i=0;i
我尝试返回对象数组和字符串数组,但都没有效果。您可以尝试将数据序列化为json并在客户端上反序列化。jQuery内置了json函数。我已经用更复杂的对象完成了这项工作,但不是用字符串数组,尽管我敢打赌它也同样容易工作。您可以直接从activeX方法发送JavaScript数组,您的函数将是:
public ArrayObject getAllFriendlyNames()
{
//.... the same ......
return Microsoft.JScript.GlobalObject.Array.ConstructArray(friendlyNames);
}
将Microsoft.JScript引用添加到项目中
MSDN:
public ArrayObject getAllFriendlyNames()
{
//.... the same ......
return Microsoft.JScript.GlobalObject.Array.ConstructArray(friendlyNames);
}