We have an existing web site and would like to add some simple Remote Procedure Calls out of our JavaScript back to a server object. As of yet and after an extensive search on the internet I have been unable to find an articles on this.
Currently my best approach was to have a RPC back into the module that created the client script. The following code (implemented on the client side JavaScript event handler for a button) will actually invoke the server side object:
var objXMLDOM = new ActiveXObject( 'Microsoft.XMLDOM' );
var objXMLHttp = new ActiveXObject( 'Microsoft.XMLHTTP' );
var strURL = document.location.href + '&RPC=true';
var strXML = '';
objXMLHttp.Open( 'POST', strURL, false );
objXMLHttp.Send( strXML );
var strResponseText = new String( objXMLHttp.responseText );
if (( objXMLHttp.status != 200 ) ||
( (objXMLDOM.loadXML( strResponseText )) == false ))
{
alert( 'Comm Problem' );
return;
}
//
// Process contents of strResponseText
//
The server side Page_Init logic looks for the ‘RPC’ reference in the Request.QueryString and will execute code that will create an XML document to send back to the client code which it does. However, on the client side code I find that the XMLHttp.responseText contains all of the client HTML as well as my XML Document I had created, not quite the response I was expecting. The server side code is as follows:
protected void Page_Init(object sender, System.EventArgs e)
{
if (Request.QueryString.ToString().ToLower().IndexOf("&rpc") != -1)
{
var sXMLDataIsland = "" + "Hello World!";
sXMLDataIsland += "'";
sXMLDataIsland += "";
Response.Clear();
Response.ContentType = "text/XML";
Response.Write(sXMLDataIsland);
}
}
I suspect that the problem is that since the object being RPC(ed) to is the object that initially created the client presentation code (.ascx) and I am not able to stop the invocation of the other methods in the object. I suppose the simplest solution would be to somehow stop the server side object from doing anything else after my Response.Write(sXMLDataIsland).
Another solution might be to have the JavaScript call a different object as opposed to the captured URL (var strURL = document.location.href + '&RPC=true') which by the way contains something like ‘http://localhost/Quoter/tabid/56/ct...1&RPC=true’ and have it redirect to an object which could hold any other RPC(s) that are needed. This other object would contain just RPC logic that would create Response.Write calls to push data back down to the client. The problem I had/have with this approach is that I can’t find a way to invoke the object back on the server.
Any ideas or thoughts on how to resolve this dilemma is greatly appreciated.