Do you need to do a webservice or can you just use normal ajax?
Since the draw of jQuery seems to be that ASP AJAX is REALLY slow [and it is terrible] I'd think that you don't actually need webservice, just a faster form of AJAX. jQuery does do this really well. I've used it extensively in a module. Any time I need AJAX in a module, I now use jQuery to do it.
My jQuery looks something like this
jQuery.ajax({
type: "POST",
async: "false",
url: location.href,
dataType: "json",
data: ({'FUNCTION': 'FunctionName', 'param0': '1' }),
success: function(data) {
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
},
beforeSend: function(xhr) {
xhr.setRequestHeader("X-OFFICIAL-REQUEST", "TRUE");//Used to ID as a AJAX Request
},
complete: function(XMLHttpRequest, textStatus) {
}
});
My server side code to deal with the postback looks something like
protected void Page_Load(object sender, EventArgs e)
{
if(Request.Headers["X-OFFICIAL-REQUEST"] == "TRUE") AjaxWrapper();
...
}
protected void AjaxWrapper()
{
Response.Clear();
string strData = "";
try
{
MethodInfo mi = GetType().GetMethod(Request.Params["FUNCTION"],
BindingFlags.Instance | BindingFlags.NonPublic);
object[] objs = new object[mi.GetParameters().Length];
for (int i = 0; i < objs.Length; i++)
{
objs[i] = (new PortalSecurity()).InputFilter(Request.Params["param" + i], PortalSecurity.FilterFlag.NoMarkup);
}
strData = mi.Invoke(this, objs).ToString();
}
catch (Exception ex)
{
strData = "{Error : Caught}";
Exceptions.ProcessModuleLoadException(this, ex);
}
Response.Write(strData);
Response.Flush();
try { Response.Close(); }catch { }//It likes to break sometimes - and other times it's needed
Response.End();
return;
}
Hopefully I can explain how this works intelligently...
If you have a function, we'll call it 'MyFunc', with 3 parameters - the jQuery data line would like like
data: ({'FUNCTION': 'MyFunc', 'param0': 'Data', 'param1': 'Goes', 'param2': 'Here' }),
Except with actual data as the parameters.
In my use of this, I have discovered a couple important things:
Your function, MyFunc, can only have strings as parameters. There are probably ways around this, but I found the simplest way was to have the parameters as strings and convert them to new variables in the function.
The other is that you have to pass in, as data, any input from the user that you need. This method basically calls a new page, so there is no user input.
Your function then returns a JSON of data, that you can then use on the client side to update whatever you need to.
A very very simple example of a function is
protected string FunctionName(string param)
{
Response.ContentType = "text/json";\\<-- Important
return "{'string' : 'Not yet implemented. \\'" + param + "\\''}";
}
This is the stripped down version. I have a couple other things [constants and such] that I use when I do this, but it has worked well for me.
Hopefully this makes sense - I'll try to keep an eye on this thread so I can answer any questions that come up.