Ajax Function: Call Code-behind C# Method



you need to create one .aspx page with the following code.

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.
  

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AjaxExample.aspx.cs" Inherits="ExampleProgram.AjaxExample" %>
<!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: "AjaxExample.aspx/ReceiveData",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
$("#receviceData").text(response.d);
},
failure: function (response) {
alert(response.responseText);
},
error: function (response) {
alert(response.responseText);
}
});
});
</script>
</head>
 
<body>
 
<form id="frmAjax" method="post">
<span id="receviceData">
</div>
</form>
</body>
</html>
 
 
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 ExampleProgram
{
public partial class AjaxExample : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static string ReceiveData()
{
return "Hello , I am rajan Singh";
}
}
}

0 Comment's

Comment Form