What we want to talk about today is that the client accesses WebService-serialization and deserialization.
 
I heard serialization in ASP. NET AJAX for the first time.
 
====================================== Demo1 ==================== ==========
 
Demo1 demonstrates using JSON characters on the client.
 
Add reference first
 
 
 <Asp: ScriptManager ID = "ScriptManager1" runat = "server"> 
<Services> 
<Asp: ServiceReference Path = "ColorService. asmx" InlineScript = "true"/> 
</Services> 
</Asp: ScriptManager> check how Javascript calls WebService. The data type here is Complex. <Script language = "javascript" type = "text/javascript"> 
Function ReverseColor () 
{ 
// Serialized JSON characters 
VaR color = {"red": 50, "green": 100, "blue": 200 }; 
Colorservice. Reverse (color, onsucceeded ); 
} 
 
Function onsucceeded (result) 
{ 
Alert (String. format ( 
"Red: {0} \ nGreen: {1} \ nBlue: {2 }", 
Result. Red, 
Result. Green, 
Result. Blue )); 
} 
</Script> 
Var color = {"Red": 50, "Green": 100, "Blue": 200}. This line is to assign the color type to the JSON character.
A little unaccustomed! But it doesn't matter. The next Demo is a familiar method, and onSucceeded is a successful callback function.
 
Let's see how ColorService is defined:
  
 [ScriptService] 
Public class ColorService: System. Web. Services. WebService 
{ 
[WebMethod] 
[GenerateScriptType (typeof (Color)] // verify 2_ComplexTypeProxy.aspx 
Public Color Reverse (Color color) 
{ 
Return new Color ( 
(Byte) (255-color. Red ), 
(Byte) (255-color. Green ), 
(Byte) (255-color. Blue )); 
} 
 
} 
It turns out that the Reverse method receives a Color parameter and returns a Color type, which is indeed very simple.
Finally, let's take a look at how the Color class is defined:
  Public class Color 
{ 
Public Color (){} 
 
Public Color (byte red, byte green, byte blue)
{
This. Red = red;
This. Green = green;
This. Blue = blue;
}
 
Public byte Red;
 
Public byte Green;
 
Public byte Blue;
}
 
This is no longer necessary. Let's go directly to Demo2.
 
====================================== Demo2 ==================== ==========
 
Demo2 describes how to generate complex types of proxies on the client and view the Javascript code on the page.
 <Script language = "javascript" type = "text/javascript"> 
Function ReverseColor () 
{ 
 
// Mark before the Service method, [GenerateScriptType (typeof (Color)] 
Var color = new ComplexType. Color (); 
Color. Red = 50; 
Color. Green = 100; 
Color. Blue = 200; 
ColorService. Reverse (color, onSucceeded ); 
} 
 
Function onSucceeded (result) 
{ 
Alert (String. format ( 
"Red: {0} \ nGreen: {1} \ nBlue: {2 }", 
Result. Red, 
Result. Green, 
Result. Blue )); 
} 
</Script> 
 
 
Aha, var color = new ComplexType. Color ();
Color. Red = 50;
Color. Green = 100;
Color. Blue = 200;
 
We are familiar with this writing method. However, if you write this method, you need to make the following annotations on the Service end:
[GenerateScriptType (typeof (Color)]. You can mark it before the class or the method name (recommended)
Just like the following code:
 [ScriptService] 
Public class ColorService: System. Web. Services. WebService 
{ 
[WebMethod] 
[GenerateScriptType (typeof (Color)] // verify 2_ComplexTypeProxy.aspx 
Public Color Reverse (Color color) 
{ 
Return new Color ( 
(Byte) (255-color. Red ), 
(Byte) (255-color. Green ), 
(Byte) (255-color. Blue )); 
} 
 
} 
============================== Demo3 ============== ====================
 
Okay, let's make a small change to Demo2.
 <Script language = "javascript" type = "text/javascript"> 
Function ReverseColor () 
{ 
 
// Mark before the Service method, [GenerateScriptType (typeof (Color)] 
Var color = new Object (); 
Color. _ type = ComplexType. Color; 
Color. Red = 50; 
Color. Green = 100; 
Color. Blue = 200; 
Colorservice. Reverse (color, onsucceeded ); 
} 
 
Function onsucceeded (result) 
{ 
Alert (String. format ( 
"Red: {0} \ nGreen: {1} \ nBlue: {2 }", 
Result. Red, 
Result. Green, 
Result. Blue )); 
} 
</Script> 
 
 
Others remain unchanged, and the same effect can be achieved.
 
Demo4 describes how to call a Table-type Service on the client; Demo5 describes how
 
Call a Service with a loop call. These two demos are complex and will not be described here. You can refer to the relevant materials.
 
Or Email me, and I will give you an answer. (Email: gfreesky@gmail.com)