Click here to Skip to main content
15,898,222 members
Articles / Web Development / ASP.NET

jQuery AJAX and HttpHandlers in ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.20/5 (4 votes)
20 Mar 2011CPOL2 min read 66.7K   1K   19   6
In this article, we will see how we can make use of the jQuery library to make AJAX calls in ASP.NET with the help of HttpHandlers.

Introduction

In this article, we will see how we can make use of the jQuery library to make AJAX calls in ASP.NET with the help of HTTPHandlers. We will also see the use of JSON (JavaScript Object Notation) which is a format describing name/value pairs in a structured manner.

Asynchronous JavaScript and XML (AJAX) is a method which we can use to exchange data with the server without reloading the complete page.

jQuery is a JavaScript library which makes it easier to manipulate and navigate DOM elements in a web page. It also helps in implementing AJAX in rapid web application development. Currently, we have Intellisense support for jQuery in Visual Studio 2008/2010.

HTTPHandler is a process which returns data when its URL is called. The returned data can be anything from a simple string to data formats like JSON or XML.

References: jQuery, MSDN

Prerequisite

Download the latest version of jQuery from the jQuery site (http://jquery.com/). In Visual Studio 2010, it is added by default when a new web application is created.

Image 1

Flow Diagram

Example: Making a Simple AJAX Call

In this example, we will make a simple AJAX call with the help of the jQuery.ajax API to an HTTPHandler. The response from the HTTPHandler will be the JSON response which is then updated in the web page. JavaScript Object notation (JSON) is a format of key/value pairs to store data. It’s easy to consume in JavaScript and compact than XML. So let’s start with creating a handler page.

C#
public void ProcessRequest (HttpContext context) {
    string method = context.Request.QueryString["MethodName"].ToString();
    context.Response.ContentType = "text/json";
    switch (method)
    {
        case "GetData" :
            context.Response.Write(GetData());
            break;
    }
}

protected string GetData()
{
    return (@"{""FirstName"":""Ravi"", ""LastName"":""Baghel"", 
               ""Blog"":""ravibaghel.wordpress.com""}");
}

As you can see, we are simply putting a response of JSON type with sample data. In order to demonstrate passing a parameter to a handler, I have used the querystring methodname which will simply switch to the method you want to execute.

Now, we need to make a call to the handler.

HTML
<head runat=""server"">
    <title>jQuery</title>
    <script src="Scripts/jquery-1.5.min.js" type="text/javascript"></script>
        <script type="text/javascript" >
    jQuery(function() {
        $(‘#btnClick’).click(function(){
            jQuery.ajax({
                type : "GET",
                url : "Data.ashx",
                data : "MethodName=GetData",
                success : function(data){
                    $(‘#display’).html("<h1> Hi, " + data.FirstName + " " + 
                      data.LastName + " your Blog Address is http://" + 
                      data.Blog + "</h1>");
                   }
        });
        });
    });
</script>
</head>
<body>
    <form id="form1″ runat=""server"">
    <input id="btnClick" type="button" value="Ajax Call" />
    <div id="display">
    </div>
    </form>
</body>

We first need to add a reference to the jQuery library which we downloaded as:

HTML
<script src="Scripts/jquery-1.5.min.js" type="text/javascript"></script>

If you look at the jQuery.ajax syntax, you can see different parameters as:

  • Type that describes whether the request is GET or POST
  • URL of the HTTPHandler
  • Data specifying the querystring
  • Success defining a function which manipulates the response from the handler

Download the latest version of the project here.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) iGATE Global Solutions Ltd
India India
I started programming at the age of 16 with BASIC. I did my Diploma in Computer Software from Aptech Computer Education in 2002, where i learned Microsoft Office, C, C++, Javascript, VB 6.0, ASP. Currently hold a Bachelor of Engineering degree in Electronics and Telecommunication from RITEE, Raipur, Chhattisgharh, pass out of 2008 batch. I currently work with Microsoft’s .NET and SQL Server Technologies.

Comments and Discussions

 
QuestionjQuery AJAX and HttpHandlers in ASP.NET Pin
Member 122364348-Sep-23 6:03
Member 122364348-Sep-23 6:03 
GeneralMy vote of 3 Pin
Tiwari Avinash19-Aug-15 2:17
Tiwari Avinash19-Aug-15 2:17 
QuestionHttpHandler Vs WebMethods Pin
Member 422822111-Dec-12 1:01
Member 422822111-Dec-12 1:01 
Questionthis code works only one time Pin
wh_rabbit21-Mar-11 16:33
wh_rabbit21-Mar-11 16:33 
AnswerRe: this code works only one time Pin
Ravi_Baghel22-Mar-11 5:10
Ravi_Baghel22-Mar-11 5:10 
GeneralRe: this code works only one time Pin
wh_rabbit22-Mar-11 14:53
wh_rabbit22-Mar-11 14:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.