SoFunction
Updated on 2025-03-06

How to use ajax request in C#

Introduction to ajax

Ajax, "Asynchronous Javascript And XML" (asynchronous JavaScript and XML), refers to a technology that creates interactive, fast dynamic web applications, and can update some web pages without reloading the entire web page.

By exchanging a small amount of data with the server in the background, Ajax can enable asynchronous updates on web pages. This means that a part of the page can be updated without reloading the entire page.

How to use ajax in C#

1. First download it, you can download it on Baidu! Search for it yourself.

2. Import into the project. Right-click the project --> Add reference ---> Browse, find the downloaded file, click OK, at this time, there is an additional bin folder in the project directory, and there is a file inside, which proves that the introduction has been successful.

3. Set configuration files.

Add the following code to the <> node under the file:

<httpHandlers> 
 <add verb="POST,GET" path="ajax/*.ashx" type=", Ajax" />
</httpHandlers> 

4. Use demo:

4.1 First, register ajax. Register ajax in the Page_Load method in the code. The registration method is (typeof(namespace.class name)). If there is no namespace, you can write the class name directly. The code is as follows:

public partial class ObjManage :  
{ 
 protected void Page_Load(object sender, EventArgs e) 
 { 
 (typeof(ObjManage)); 
 } 
}

4.2 Write a method of cs for javascript to call. The front end of the cs method must have [], and then the method must be public public or static. For example:

[] 
 public static string getString(string str) 
 { 
 string strResult = "The string is " + str; 
 return strResult; 
 } 

4.3 JavaScript calls the cs method. The format of the call is: class name, method name (parameter), for example:

function alertString() { 
  var str = ("myAjax").value; 
  alert(str); 
 } 

This is done. This has passed the test. If there is any problem, please leave a message. The completed source code is given below, but the code is not given. You will be OK when installing the third step of setting the configuration file for setting it. cs code:

using System; 
using ; 
using ; 
using ; 
using ; 
using ; 
 
public partial class ObjManage :  
{ 
 protected void Page_Load(object sender, EventArgs e) 
 { 
 (typeof(ObjManage)); 
 } 
 
 [] 
 public static string getString(string str) 
 { 
 string strResult = "The string is " + str; 
 return strResult; 
 } 
}

aspx code:

&lt;%@ Page Language="C#" AutoEventWireup="true" CodeFile="" Inherits="ObjManage" %&gt; 
 
&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:///TR/xhtml1/DTD/"&gt; 
 
&lt;html xmlns="http:///1999/xhtml"&gt; 
&lt;head runat="server"&gt; 
 &lt;title&gt;&lt;/title&gt; 
 &lt;script type="text/javascript"&gt; 
 function alertString() { 
  var str = ("myAjax").value; 
  alert(str); 
 } 
 &lt;/script&gt; 
&lt;/head&gt; 
&lt;body&gt; 
 &lt;form  runat="server"&gt; 
 &lt;div&gt; 
 &lt;input type="button" value="Get information" onclick="alertString();" /&gt; 
 &lt;/div&gt; 
 &lt;/form&gt; 
&lt;/body&gt; 
&lt;/html&gt;

The above is the detailed content of how to use ajax request in C#. For more information about using ajax request in C#, please follow my other related articles!