jQuery Web Services
Use jQuery to consume ASP.NET web services. This code removes the dependency on ASP.NET AJAX when consuming ASP.NET Web Service.
We start of by making our web service, in this case UserService.
[ScriptService]
public class UserService : System.Web.Services.WebService
{
[WebMethod]
public User[] GetUsers()
{
return new User[]
{
new User
{
Name = "thorsteinsson"
},
};
}
}
Then we include the js proxy for the service and my jQuery web service file (you need jquery as well).
<script type="text/javascript" src="json2.js"></script> <script type="text/javascript" src="jquery.webservices.js"></script> <script type="text/javascript" src="UserService.asmx/js"></script>
Now the web service can be consumed as if we have asp.net ajax included.
WebApplication1.UserService.GetUsers(function(users) {
alert(users[0].Name);
});
The project is on github: http://github.com/thorsteinsson/jquery-webservices
Crockfords JSON parser: http://www.JSON.org/json2.js

