[英]Pass Array From C# COM object to JavaScript?
與此類似如何將字符串數組從 ActiveX 對象返回到 JScript但在 C# 中。
我有一個 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];
輸出“未定義” 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;
}
我試過返回對象數組和字符串數組都無濟於事。
您可以直接從您的 activeX 方法發送 JavaScript 數組,您的函數將是:
public ArrayObject getAllFriendlyNames()
{
//.... the same ......
return Microsoft.JScript.GlobalObject.Array.ConstructArray(friendlyNames);
}
將 Microsoft.JScript 引用添加到您的項目中。
您可以嘗試將數據序列化為 json 並在客戶端反序列化。 jQuery 內置了 json 函數。 我已經使用更復雜的對象完成了此操作,但沒有使用字符串數組,盡管我敢打賭它會同樣輕松地工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.