ASP.NET AJAX heavily uses web services to perform
client-server communication. There are multiple ways to create a web service
that the client can connect with. The one way I'll touch upon here is the
System.Net.WebServiceProxy class. This class is the proxy to make a call to a
method on a web service. Whether the web service succeeds or fails, the
appropriate method call is made.
As a simple test, I defined a WebMethod inside of my page as
such:
Listing 15
[WebMethod]
public static string GetOutput()
{
return "Successful";
}
The following markup simply makes the call to the page
method and supplies the label with the text value returned from the web service
method, if no errors occurred. Otherwise, if errors did occur, the text
"Error" appears from the onFailure callback.
Listing 16
<script language="javascript" type="text/javascript">
function pageLoad()
{
var wsProxy = new Sys.Net.WebServiceProxy();
wsProxy.set_timeout(0);
var label = $get("output");
var request = wsProxy._invoke("http://localhost:61379/testloader.aspx",
"GetOutput", false, {}, onSuccess, onFailure, label);
}
function onSuccess(result, userContext, methodName)
{
userContext.innerText = result;
}
function onFailure(result, userContext, methodName)
{
userContext.innerText = "Error";
}
</script>
<span id="output"></span>
Running this page makes the call to the web service and
returns the result to the label. A nice feature of Visual Studio 2008 is that
you can debug both the server-side and client-side call to ensure it works correctly.