I'd like to pass an associative array (or simply an object with property names & values) to my ActiveXObject. I can't find anyone who has successfully and simply passed complex data from javascript to an ActiveX object.
My ActiveX object is being loaded in IE, and it's mine so I can change the method signature & code to whatever will work. I also have control over the structure of the javascript.
Without a simple way of doing this I'm thinking of url-开发者_如何转开发encoding the data and sending it as a string. 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...
I found the best way to do this was to send JSON strings back & forth. 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.
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.
Alternatively, for real simple cases where you don't want a JSON parser you can use use URL-encoded strings.
My ActiveX/JavaScript knowledge is a little dated, but so is ActiveX...
Ideally you'd want to pass in a Object
that you use as associative array. But COM does not recognize this type. COM does support SafeArray
s but they are not understood by JavaScript.
We ended up sending either join()
ed strings, or sending simple Xml documents. 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).
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. 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. It's a Microsoft Jscript specific implementation, but since you're using ActiveX it may be worthwhile looking into.
精彩评论