Pass Array From C# COM object to JavaScript?(将数组从 C# COM 对象传递给 JavaScript?)
问题描述
类似于此 如何将一个字符串数组从 ActiveX 对象返回到 JScript,但在 C# 中.
Similar to this How do I return an array of strings from an ActiveX object to JScript but in C#.
我有一个 COM 控件,可以将字符串数组传回 javascript.似乎 javascript 无法理解我要传回的内容,并且 javascript 中的数组始终未定义.
I have an COM control that passes back an array of strings to javascript. It seems like javascript cant understand what it is I am passing back and the array in javascript is always undefined.
Javascript:
Javascript:
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 object[] getAllFriendlyNames()
{
if (!keystoreInitialized)
throw new Exception("Key store has not been initialized");
X509Certificate2Collection allCerts = certificateStore.Certificates;
int storeSize = allCerts.Count;
if (storeSize == 0)
throw new Exception("Empty Key Store, could have opened using the wrong keystore name.");
object[] friendlyNames = new object[storeSize];
for (int i = 0; i < storeSize; i++)
{
string friendlyName = allCerts[i].FriendlyName;
if (friendlyName == "")
friendlyName = allCerts[i].Subject;
friendlyNames[i] = (object) friendlyName;
}
return friendlyNames;
}
我试过返回对象数组和字符串数组都无济于事.
I've tried returning both object arrays and string arrays to no avail.
推荐答案
您可以尝试将数据序列化为 json 并在客户端进行反序列化.jQuery 内置了 json 函数.我已经用更复杂的对象做到了这一点,但没有用字符串数组,虽然我敢打赌它会很容易地工作.
You can try serializing your data to json and deserializing on the client. jQuery has built in json functions. I've done this with more complex objects, but not with string arrays, though I'd bet that it will work just as easily.
这篇关于将数组从 C# COM 对象传递给 JavaScript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将数组从 C# COM 对象传递给 JavaScript?


基础教程推荐
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01