Introduction to ASHX files with JavaScript and AJAX call



Hello folks,

This tutorial talks about ASHX files and how we can use them as server handlers. We will access the handler files by using JavaScript and  AJAX calls.

Let's get started!



In this article, let's take an example of sending a text message from your HTML page to the server.

Step 1

Following is an example of the HTML page along with the JavaScript:
 <!DOCTYPE html>  
 <html>  
   
 <head>  
   <script type="text/javascript">  
     function SendDataToServer() {  
       var stringFromHtml = document.getElementById('htmlTextBox').value;  
       $.ajax({  
         url: "Services/Main.ashx",  
         contentType: "application/json; charset=utf-8",  
         data: {  
           stringToSend: stringFromHtml  
         },  
         success: function(result) {  
                          alert(result);  
         },  
         error: function OnFail(result) {  
           alert(result);  
         }  
       });  
     }  
   </script>  
 </head>  
   
 <body>  
   <input type="text" name="htmlTextBox" id="htmlTextBox">  
   <a href="" onclick="SendDataToServer()">Send data</a>  
 </body>  
   
 </html>  
Now it is time to reference the handler file.

Step 2

I have created a folder in my project called 'Services'. Inside that folder, let us create our handler file and name it 'Main.ashx'.

We need to put in the C# code which will be executed inside the handler.

Your handler will look something like this:
 <% @WebHandler Language = "C#"  
 Class = "Main" %>  
   
 using System;  
 using System.Web;  
 using System.Linq;  
 using System.Text;  
 using System.Collections.Generic;  
 using System.Data;  
 using System.Data.SqlClient;  
   
 public class Main: IHttpHandler,  
 System.Web.SessionState.IRequiresSessionState {  
   
      public void ProcessRequest(HttpContext context) {  
           context.Response.ContentType = "text/plain";  
   
           string stringInServer = context.Request.QueryString["stringToSend"].ToString();  
           string result = null;  
           string con = "your connection string";  
           try {  
                string query = "INSERT into tablename (stringValue) " + " VALUES ('" + stringInServer + "');";  
                cmd = new SqlCommand(query, con);  
                cmd.ExecuteNonQuery();  
   
                result = "Done";  
           } catch (Exception ex) {  
                result = ex.Message;  
           }  
           context.Response.Write(result);  
      }  
 }  
 public bool IsReusable {  
      get {  
           return false;  
      }  
 }  
 }  
The above code captures the string from the HTML page and saves it in the database. Make sure to put your connection string and table name/columns in the above code.

Finally

Remember you need to have a windows hosting in order to use ASHX handler files. Linux hosting would not support these files.

The above code can also be used to send images to the server from your android application. You can check this article for the tutorial.

Peace and cheers!

Popular posts from this blog

Building your first Phonegap android application with HTML and JavaScript