将数组从C#COM对象传递给JavaScript? [英] Pass Array From C# COM object to JavaScript?

查看:70
本文介绍了将数组从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]输出'undefined' ;

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆