Call ASP.NET Handler (ASHX) using JavaScript

I was working on an application which used AJAX and needed the XMLHttpRequest object to communicate with an ASP.NET Generic Handler (.ashx), which contained some server-side logic. Here’s a working prototype if you have a similar requirement

We will first create an ASP.NET handler (ashx) which accepts a ‘Name’ parameter and greets the user by appending ‘Hello’ to it.

We will then call this ASP.NET Handler (ashx) via JavaScript by passing in a value entered in an ASP.NET TextBox.

In the code shown above, on the asp.net button click event, we call the callASHX() javascript function. In this function, we first initialize our XMLHttpRequest object by calling the XMLHttpRequest() function and asynchronously invoke an .ashx file called SayHello.ashx and pass the textbox value to it. If the request to the .ashx handler completes successfully, the XMLHttpRequestCompleted() function is invoked and the user is greeted.