2010-07-28 62 views
3

與此類似How do I return an array of strings from an ActiveX object to JScript但在C#中。傳遞數組從C#COM對象到JavaScript?

我有一個COM控件,將字符串數組傳回給javascript。它似乎是JavaScript不知道它是什麼,我回傳和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; 
    } 

我試圖返回兩個對象數組和字符串數組無濟於事。

回答

2

你可以嘗試序列化你的數據到json和反序列化在客戶端上。 jQuery內置了json函數。我已經用更復雜的對象完成了這個任務,但是沒有使用字符串數組,但我敢打賭,它的工作方式會很簡單。

2

您可以從您的ActiveX方法直接發送JavaScript數組,你的函數將是:

public ObjectArray getAllFriendlyNames() 
{ 
    //.... the same ...... 
    return Microsoft.JScript.GlobalObject.Array.ConstructArray(friendlyNames); 
} 

添加Microsoft.JScript程序引用到您的項目。