I am trying to convert an AjaxObject.messages
to a JSON string and send it out to another function from a JavaScript code.
This message should be received by a C# method.
If I send AjaxObject.messages.tradeSchedule.binShares.length
instead of AjaxObject.messages
I get the correct value in the C# method.
Now I want to send the entire message instead of just the length of a JSON string.
Is there a way to convert that AjaxObject.messages
to a JSON string and send it?
Here is the code:
private string CreateRequestCompleteListenerScript()
{
StringBuilder script = new StringBuilder();
script.AppendLine("var RequestCompleteListener = function(callback) {");
script.AppendLine(" var internalCallback = {");
script.AppendLine(" handleCallback:function(message, params){");
script.AppendLine(" if (message == null){");
script.AppendLine(" callback.HandleNullMessage();");
script.AppendLine(" } else {");
script.AppendLine(" callback.HandleRequestComplete(AjaxObject.messages);");
script.AppendLine(" }");
script.AppendLine(" }");
script.AppendLine(" }");
script.AppendLine(" UpdateAjaxObject.addListener('requestcomplete', internalCallback.handleCallback, internalCallback);");
script.AppendLine("};");
return script.ToString();
}
//AjaxObject.messages.tradeSchedule.binShares.length
private void SendJavaScript(string script)
{
theBrowser.Document.InvokeScript("eval", new object[] {script});
}
[System.Runtime.InteropServices.ComVisible(true)]
public class CallbackHandler
{
public CallbackHandler(VoidHandler requestCompleteAction, VoidHandler nullMessageAction)
{
_requestCompleteAction = requestCompleteAction;
_nullMessageAction = nullMessageAction;
}
private VoidHandler _requestCompleteAction;
private VoidHandler _nullMessageAction;
private bool _requestCompleted;
[System.Runtime.InteropServices.DispId(0)]
public void HandleRequestComplete(object messages)
{
JavaScriptSerializer js = new JavaScriptSerializer();
string json = js.Serialize(messages);
_log.Info(json);
if (!_requestCompleted)
_requestCompleteAction();
_requestCompleted = true;
}
This is how the AjaxObject looks like:
js {System.Web.Script.Serialization.JavaScriptSerializer} System.Web.Script.Serialization.JavaScriptSerializer
json "{}" string
messages {System.__ComObject} object {System.__ComObject}
base {System.__ComObject} System.MarshalByRefObject {System.__ComObject}
- [System.__ComObject] {System.__ComObject} System.__ComObject
- Non-Public members
- Dynamic View Expanding the Dynamic View will get the dynamic members for the object
- Non-Public members
- Dynamic View Expanding the Dynamic View will get the dynamic members for the object