jQuery AJAX and HttpHandlers in ASP.NET

https://www.codeproject.com/Articles/170882/jQuery-AJAX-and-HttpHandlers-in-ASP-NET

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.

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.

复制代码
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. 

 

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(169)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2018-06-21 JavaScript面试题
2018-06-21 JavaScript中的string interpolation
2018-06-21 visual studio , JavaScript , UnitTest
点击右上角即可分享
微信分享提示