In this article, we will learn how to implement an ajax method in jQuery. There is no need to explain that ajax is one of the strong pillars of the future of web development. Since ajax is a concept or technique we can implement ajax with any web development platform. It is possible to implement ajax with PHP or with JSP or with ASP.NET or with many other technologies. Since we are learning .NET, in this article we will explain ajax in the context of C# .NET.
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="JavaScript.aspx.cs" Inherits="WebApplication.JavaScript" %>
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
- <script>
- $(document).ready(function () {
- $.ajax({
- type: "POST",
- url: "JavaScript.aspx/GetData",
- contentType: "application/json; charset=utf-8",
- dataType: "json",
- success: function (response) {
- $("#Content").text(response.d);
- },
- failure: function (response) {
- alert(response.d);
- }
- });
- });
- </script>
- </head>
- <body>
- <form id="frm" method="post">
- <div id="Content">
- </div>
- </form>
- </body>
- </html>
- type: This is nothing but a request type. Here we are making a POST request type. We will explain various types in the following articles.
- URL: This is nothing but the location of a C# function. Here we have provided a page name/path at first and then the method name followed by '/'. We can provide the URL of the remote server.
- data-type: This defines in which form the data will pass. In other words, what the encoding scheme is for the data passed in. For example, we can send data in JSON or XML format.
- Success and failure: Both are callback functions, after a successful ajax request the success function will execute and after failure, the failure function will execute.
Within the success function, we are getting data and pushing it to the DIV tag that was defined in the body portion of the HTML page. Step 2: Define C# function in code-behind Now we need to define the GetData() method in code-behind. Go to the code-behind portion of the same page and define the following method.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.Services;
- namespace WebApplication
- {
- public partial class JavaScript : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- [WebMethod]
- public static string GetData()
- {
- return "This string is from Code behind";
- }
- }
- }
- [WebMethod]
- public static string GetData()
- {
- Dictionary<string, string> name = new Dictionary<string, string>();
- name.Add("1", "Sourav Kayal");
- name.Add("2", "Ram mishra");
- string myJsonString = (new JavaScriptSerializer()).Serialize(name);
- return myJsonString;
- }
Step 2: Define JQuey ajax function Here is the definition of the jQuery ajax function. The code is very similar to the example above.
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="JavaScript.aspx.cs" Inherits="WebApplication.JavaScript" %>
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
- <script>
- $(document).ready(function () {
- $.ajax({
- type: "POST",
- url: "JavaScript.aspx/GetData",
- contentType: "application/json; charset=utf-8",
- dataType: "json",
- success: function (response) {
- var names = response.d;
- alert(names);
- },
- failure: function (response) {
- alert(response.d);
- }
- });
- });
- </script>
- </head>
- <body>
- <form id="frm" method="post">
- <div id="Content">
- </div>
- </form>
- </body>
- </html>
Here is the sample example. In this article, we saw how to implement a simple jQuery ajax method to call a code-behind C# method. In a future article, we will learn to call any service method from the jQuery ajax method.
No comments:
Post a Comment