What is the most efficient way of calling some business logic from javascript on the client side using AJAX? It looks like you can call a [WebMethod] on an aspx directly from javascript (in my case I'm using JQuery to help out) OR you can call a .asmx directly. Which call incurs less overhead? What is the best practice?
Also, what does the [ScriptService] attribute do on a class? I have never used this before on my .aspx [WebMethod] methods and everything seems to be working fine.
I'm hoping this is a purely objective question. Thanks in advance!
AJAX is a technique for creating fast and dynamic web pages. AJAX allows web pages to be updated asynchronously by exchanging small amounts of data with the server behind the scenes. This means that it is possible to update parts of a web page, without reloading the whole page.
An ASHX is a generic HttpHandler. An ASMX file is a web service. ASHX is a good lean way to provide a response to AJAX calls, but if you want to provide a response which changes based on conditions (such as variable inputs) it can become a bit of a handful - lots of if else etc.
jQuery allows you to call server-side ASP.NET methods from the client-side without any PostBack. Actually, it is an AJAX call to the server but it allows us to call the method or function defined server-side. The figure below describes the syntax of the call. $.ajax({
The ScriptService stuff in my opinion is a hidden gem in asp.net. Calls to the script service do not passback form data + viewstate, they are lean, fast JSON payloads.
Heres the best part, ASP.NET3.5's scriptmanager can do most of the work for you regarding generating a JS method for you to call and also setting up any JS classes needed.
A simple example for fetching details for a "Person", assuming Person is a C# class.
In PersonService.asmx:
namespace MyProj.Services { [System.Web.Script.Services.ScriptService] [System.Web.Script.Services.GenerateScriptType(typeof(Person))] public class PersonService : System.Web.Services.WebService { [WebMethod, ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)] public Person GetPersonDetails(int id) { /* return Logic here */ } } }
In DetailsPage.aspx
<asp:ScriptManager ID="ScriptManager1" runat="server"> <Services> <asp:ServiceReference Path="~/Services/PersonService.asmx" /> </Services> </asp:ScriptManager>
By using a setup like this, you won't even need the help of JQuery to call the service and get back a JS version of your C# Person class, .net does that all for you. An example of using this service from JS would be:
MyProj.Services.PersonService.GetPersonDetails(id, _onDetailsCallbackSuccess, _requestFailed, null); _onDetailsCallbackSuccess: function(result, userContext, methodName) { //all persons properties are now intact and available document.getElementById('txtFirstname').value = result.Firtname; }
Anyway, it would be more then worth looking into the ASP.NET Ajax ScriptService stuff. Even if you decide not to use it this time it's a pretty wicked feature.
Links
If server-side overhead is all you care about, then sending a simple GET with a query string to an ASHX would probably be it. It's definitely preferable to an .ASPX, which is going to go through a page life-cycle that you don't need.
The advantage of an ASMX web method is that it's built on a standard that can be called by other technologies easily (supports discovery). With some documentation, your .ASHX will be just as easy to call, though.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With