将数组从 C# COM 对象传递给 JavaScript?

Pass Array From C# COM object to JavaScript?(将数组从 C# COM 对象传递给 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?

基础教程推荐