There are many solutions to this problem. If the content you return is simple content, such as authentication information when registering a user, you only need to return the existence or not, you can use the simplest code below:
C # code
<% @ Page Language = "C #" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN"
Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>
<Script runat = "server">
// Test method 1
Public String GetSimpleMethod (String inputData)
{
// Business processing.
Return "your input value:" + inputData;
}
Protected void Page_Load (object sender, EventArgs e)
{
If (Request. QueryString ["input"]! = Null)
{
Response. ClearContent ();
Response. Write (GetSimpleMethod (Request. QueryString ["input"]);
Response. End ();
}
}
</Script>
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head id = "Head1" runat = "server">
<Title> </title>
<Script type = "text/javascript">
Var textBox1 = "<% = TextBox1.ClientID %> ";
Function GetData (){
Var t = document. getElementById (textBox1 );
Var h = window. XMLHttpRequest? New XMLHttpRequest (): new ActiveXObject ("MSXML2.XMLHTTP ");
H. open ("GET", "<% = Request. FilePath %>? Input = "+ encodeURIComponent (t. value) +" & "+ Date. parse (new Date (), true );
H. setRequestHeader ("Connection", "close ");
H. onreadystatechange = function (){
If (h. readyState = 4 ){
If (h. status = 200 ){
Document. getElementById ("info"). innerHTML = h. responseText;
}
}
}
H. send (null );
}
</Script>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Asp: TextBox ID = "TextBox1" runat = "server"> </asp: TextBox>
<Asp: Button ID = "Button1" runat = "server" Text = "Call the server method GetSimpleMethod" OnClientClick = "GetData (); return false;"/>
<Div id = "info"> </div>
</Form>
</Body>
</Html>
To return a complex object, you need to serialize the object. You can use the following simple method. Note the following three points when using this method:
1. The background method must be marked as the [System. Web. Services. WebMethod] attribute;
2. The background method must be a static method of the static type;
3. EnablePageMethods = "true" must be set for ScriptManager ".
ASPX code
<% @ Page Language = "C #" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN"
Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>
<Script runat = "server">
// Test method 1
[System. Web. Services. WebMethod]
Public static String GetSimpleMethod (String inputData)
{
Return "your input value:" + inputData;
}
// Test method 2
[System. Web. Services. WebMethod]
Public static System. Collections. Generic. List <BlogUser> GetListObjectMethod (int inputData)
{
System. Collections. Generic. List <BlogUser> ulist = new System. Collections. Generic. List <BlogUser> ();
System. Random r = new Random ();
For (int I = 0; I <6; I ++)
{
BlogUser u = new BlogUser ();
U. UserName = "Meng xianhui" + inputData. ToString ();
U. Score = r. Next (0,100 );
Ulist. Add (u );
}
Return ulist;
}
// Return the test class.
Public class BlogUser
{
Public String UserName {set; get ;}
Public Int32 Score {set; get ;}
}
</Script>
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head id = "Head1" runat = "server">
<Title> </title>
<Script type = "text/javascript">
Var textBox1 = "<% = TextBox1.ClientID %> ";
Function GetData1 (){
PageMethods. GetSimpleMethod (document. getElementById (textBox1). value, OnSucceeded1, OnFailed );
}
Function GetData2 (){
PageMethods. GetListObjectMethod (document. getElementById (textBox1). value, OnSucceeded2, OnFailed );
}
Function OnSucceeded1 (result, userContext, methodName ){
Alert (result)
}
Function OnSucceeded2 (result, userContext, methodName ){
Alert (result)
Var html = "<table border = 1> ";
For (var I = 0; I <result. length; I ++ ){
Html + = "<tr> ";
Html + = "<td>" + result [I]. UserName + "</td> <td>" + result [I]. Score + "</td> ";
Html + = "</tr> ";
}
Html + = "</table> ";
Document. getElementById ("info"). innerHTML = html;
}
Function OnFailed (error, userContext, methodName ){
If (error! = Null ){
Alert ("Call method error:" + error. get_message ());
}
}
</Script>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Asp: ScriptManager ID = "ScriptManager1" runat = "server" EnablePageMethods = "true"/>
<Asp: TextBox ID = "TextBox1" runat = "server"> </asp: TextBox>
<Asp: Button ID = "Button1" runat = "server" Text = "Call the server method GetSimpleMethod" OnClientClick = "GetData1 (); return false;"/>
<Asp: Button ID = "Button2" runat = "server" Text = "Call the server method GetListObjectMethod" OnClientClick = "GetData2 (); return false;"/>
<Div id = "info"> </div>
</Form>
</Body>
</Html>