[英]How to pass associative Array parameter from javascript to ActiveX object?
I'd like to pass an associative array (or simply an object with property names & values) to my ActiveXObject. 我想将一个关联数组(或只是一个带有属性名和值的对象)传递给我的ActiveXObject。 I can't find anyone who has successfully and simply passed complex data from javascript to an ActiveX object. 我找不到任何成功的人,只是简单地将复杂的数据从javascript传递给ActiveX对象。
My ActiveX object is being loaded in IE, and it's mine so I can change the method signature & code to whatever will work. 我的ActiveX对象正在IE中加载,它是我的,所以我可以将方法签名和代码更改为任何可行的。 I also have control over the structure of the javascript. 我也控制了javascript的结构。
Without a simple way of doing this I'm thinking of url-encoding the data and sending it as a string. 没有一个简单的方法,我正在考虑对数据进行url编码并将其作为字符串发送。 But that does seem a little silly if it's possible just to pass an object. 但是,如果只是传递一个对象,那似乎有点傻。
The ActiveX object is coded in C# if that makes any difference... ActiveX对象用C#编码,如果这有什么不同......
I found the best way to do this was to send JSON strings back & forth. 我发现最好的方法是来回发送JSON字符串。 This is very simple for both simple data and complex data. 这对于简单数据和复杂数据都非常简单。
To send data from an ActiveXObject to javascript, just use a StringBuilder or similar to format the data as JSON. 要将ActiveXObject中的数据发送到javascript,只需使用StringBuilder或类似的方法将数据格式化为JSON。
To send data from javascript to an ActiveXObject, use JSON library within javascript to encode the data, then use a JSON parser in .net like JSON.Net to read it. 要将数据从javascript发送到ActiveXObject,请使用javascript中的JSON库对数据进行编码,然后使用.net中的JSON解析器(如JSON.Net)来读取它。
Alternatively, for real simple cases where you don't want a JSON parser you can use use URL-encoded strings. 或者,对于您不需要JSON解析器的真实简单情况,您可以使用URL编码的字符串。
My ActiveX/JavaScript knowledge is a little dated, but so is ActiveX... 我的ActiveX / JavaScript知识有点过时,但ActiveX也是如此......
Ideally you'd want to pass in a Object
that you use as associative array. 理想情况下,您需要传入一个用作关联数组的Object
。 But COM does not recognize this type. 但COM不承认这种类型。 COM does support SafeArray
s but they are not understood by JavaScript. COM确实支持SafeArray
但JavaScript并不理解它们。
We ended up sending either join()
ed strings, or sending simple Xml documents. 我们最终发送了join()
ed字符串或发送简单的Xml文档。 You probably don't need a DOM to produce xml, and on the receivign side you can feed them directly into your favorite Xml Api (in C#, i reccomend XDocument). 您可能不需要DOM来生成xml,并且在接收方面,您可以将它们直接提供给您喜欢的Xml Api(在C#中,我推荐XDocument)。
The quickest way is probably to use the Scripting.Dictionary COM object which can be accessed via JScript, then include a reference to it in your C# project (if it is not listed it will be in scrrun.dll) then specify the IDictionary interface (not the System.Collections one, the one out of the imported COM library) as the parameter to your function. 最快的方法可能是使用可以通过JScript访问的Scripting.Dictionary COM对象,然后在C#项目中包含对它的引用(如果未列出它将在scrrun.dll中),则指定IDictionary接口(不是System.Collections一个,导出的COM库中的一个)作为函数的参数。 Then you have a key-value associative dictionary at your disposal. 然后你就可以得到一个键值关联词典。
It's been a while, but I remember doing something similar with SafeArrays, and the VBArray object. 已经有一段时间了,但我记得用SafeArrays和VBArray对象做类似的事情。 It's a Microsoft Jscript specific implementation, but since you're using ActiveX it may be worthwhile looking into. 这是Microsoft Jscript特定的实现,但由于您使用ActiveX,因此可能值得研究。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.